通过非案例类的字段排序PriorityQueue(Ordering a PriorityQueue by a field of a non-case class)
我目前正在尝试使用scala实现霍夫曼算法。 要做到这一点,我想我会使用PriorityQueue来根据它们的权重对树中不同节点的排序。 因此,我必须创建一个BinarySearchTree节点的PriorityQueue。 但是,Scala只允许我通过案例类的字段进行订购。
这是我想要的:
class BinarySearchTree(weight: Int) case class ForkNode(left: BinarySearchTree, right: BinarySearchTree, chars: List[Char], weight: Int) extends BinarySearchTree(weight) case class LeafNode(char: Char, weight: Int) extends BinarySearchTree(weight) def createBST(inputFile: ListMap[Char,Int]): BinarySearchTree = { def weightOrder(t2: BinarySearchTree) = t2.weight val nodeMap:PriorityQueue[BinarySearchTree] = PriorityQueue(Ordering.by(weightOrder)) null }
但它不编译。 但是,
def weightOrder(t2: ForkNode) = t2.weight
不会编译,但这不是我想要的。我如何根据非案例类中的字段来排序我的优先级队列?
I'm currently trying to implement the huffman algorithm using scala. To do this, I'd figured I'd use a PriorityQueue for the ordering of the different Nodes in the tree based on their weight. Thus, I have to create a PriorityQueue of BinarySearchTree Nodes. However, Scala only lets me order by a field of a case class.
This is want I want:
class BinarySearchTree(weight: Int) case class ForkNode(left: BinarySearchTree, right: BinarySearchTree, chars: List[Char], weight: Int) extends BinarySearchTree(weight) case class LeafNode(char: Char, weight: Int) extends BinarySearchTree(weight) def createBST(inputFile: ListMap[Char,Int]): BinarySearchTree = { def weightOrder(t2: BinarySearchTree) = t2.weight val nodeMap:PriorityQueue[BinarySearchTree] = PriorityQueue(Ordering.by(weightOrder)) null }
But it doesn't compile. However,
def weightOrder(t2: ForkNode) = t2.weight
does compile, but that is not what I want.How can I order my priority queue based on a field in a non-case class?
原文:https://stackoverflow.com/questions/47043379
最满意答案
您可以使用闪现的会话 。 尝试这个:
public function handleProviderCallback() { try { $user = Socialite::driver('google')->user(); } catch (Exception $e) { return redirect('auth/google'); } $authUser = User::where('google_id', $user->id)->first(); if($authUser) //if user is found { Auth::login($authUser, true); return redirect('/home'); } else //if user is not found, redirect to register page { return redirect('/register')->with('user', $user); } }
并在你的注册视图
<input type='text' name='email' value='{{session("user")->email}}'>
You can use flashed session. Try this:
public function handleProviderCallback() { try { $user = Socialite::driver('google')->user(); } catch (Exception $e) { return redirect('auth/google'); } $authUser = User::where('google_id', $user->id)->first(); if($authUser) //if user is found { Auth::login($authUser, true); return redirect('/home'); } else //if user is not found, redirect to register page { return redirect('/register')->with('user', $user); } }
and in your register view
<input type='text' name='email' value='{{session("user")->email}}'>
相关问答
更多-
Laravel 5.4 - 社交网络提供商使用Twitter进行多种配置(Laravel 5.4 - Socialite Providers multiple configurations with Twitter)[2022-06-23]
我认为这将在https://packagist.org/packages/socialiteproviders/manager之后起作用 $clientId = "secret"; $clientSecret = "secret"; $redirectUrl = "yourdomain.com/api/redirect"; $additionalProviderConfig = ['site' => 'meta.stackoverflow.com']; $config = new \SocialitePro ... -
由于您的RegisterController使用了RegistersUsers特性,所有特征的方法都可用于RegisterController 。 您需要重写的方法,以防止用户在成功注册后登录的是register() 。 这是该方法的最初主体: public function register(Request $request) { $this->validator($request->all())->validate(); event(new Registered($user = $th ...
-
我已经解决了这个问题在控制器laravel 5.4中使用parsedown,我们只需要初始化parsedown类而不使用像这样的命名空间: $parsedown = new \Parsedown(); $md = $parsedown->text('# this H1 tag'); I have solved this Question to use parsedown in controller laravel 5.4, we only initial parsedown class without ...
-
您可以使用闪现的会话 。 尝试这个: public function handleProviderCallback() { try { $user = Socialite::driver('google')->user(); } catch (Exception $e) { return redirect('auth/google'); } $authUser = User::where('google_id', ...
-
Glyphicons包含在通过npm安装的bootstrap-sass中。 因此,如果要删除glyphicons,则必须在bootstrap.js文件中删除require('boostrap-sass')。 或者如果你真的只想删除字体,那么你删除 node_modules /自举萨斯/资产/字体/引导 目录,但这可能会显示一个错误,即没有找到glyphicons。 要解决上面的错误,你应该去 node_modules /自举萨斯/资产/样式表/ _bootstrap.scss 文件并删除此代码 @impor ...
-
文员在laravel 5.4(Textarea in laravel 5.4)[2023-08-16]
您需要安装Laravel FormCollective 。 从终端运行以下命令: composer require "laravelcollective/html":"^5.2.0" 接下来,将您的新提供程序添加到config/app.php的providers数组config/app.php : 'providers' => [ // ... Collective\Html\HtmlServiceProvider::class, // ... ], 最后,将两个类别名添加 ... -
我认为问题是你正在尝试为Laravel 5.3安装最新的社交网络套件,至少需要Laravel 5.4。 Problem 1 - laravel/socialite v3.0.0 requires illuminate/http ~5.4 -> satisfiable by 尝试在Laravel 5.4上安装。 检查一下: https://github.com/laravel/socialite/blob/3.0/composer.json "require": { "php": ">=5 ...
-
您User_entity必须扩展Illuminate\Foundation\Auth\User才能使登录工作。 在默认的User模型中,这是别名为Authenticatable 。 目前您正在扩展Eloquent ,但Authenticatable扩展了Illuminate\Database\Eloquent\Model ,它将为您解决这个问题。 在User_entity模型中尝试此User_entity : use Illuminate\Foundation\Auth\User as Authentica ...
-
laravel 5.4无法安装(laravel 5.4 can't install)[2022-05-30]
从您的(dev-?)依赖项中删除mpociot/laravel-test-factory-helper或将其更新为支持Laravel 5.4的版本 Remove mpociot/laravel-test-factory-helper from your (dev-?) dependencies or update it to a version that supports Laravel 5.4 -
您将无法使用查询,但如果要限制通过预先加载返回的字段,则可以使用select 。 您只需要确保包含id以便Eloquent可以正确匹配关系,例如: $something = Something::with(array('something_else' => function($query){ $query->select('id', 'field'); }))->first(); 希望这可以帮助! You wouldn't be able to use pluck on the query but ...