Mix syntax for Boolean elements - raku

If I try to declare a Mix with Boolean components:
my $mix= (True => 0.3, False => 0.7).Mix;
dd $mix; # OUTPUT: «Mix $mix = ("True"=>0.3,"False"=>0.7).Mix␤»
They use Pair syntax, which quotes automatically those bare identifiers. In order to avoid that, you either have to define the Pairs explicitly via Pair.new, or else use the fully qualified name.
my $mix= (Bool::True => 0.3, Bool::False => 0.7).Mix;
Is there any other way of doing that? A simpler way maybe?

You can use anything that isn't seen as a bare-word.
Fully qualified names work.
Bool::True => 1
The reason they work is bare-words don't have :: in them.
So you can just prepend :: as well.
::True => 1
You can use ::(…)
::(True) => 1
::('True') => 1
::< True > => 1
You can also use () around True.
(True) => 1
You could declare it backwards and use .antipair
( 1 => True ).antipair
( :foo ).antipair # (Bool::True) => 'foo'
If you don't mind getting a sequence you can use .invert, or .antipairs
# Seq.new( Bool::True => 1 )
( 1 => True ).invert
( 1 => True ).antipairs
# Seq.new( Bool::True => 1, Bool::False => 2 )
( 1 => True, 2 => False ).invert
( 1 => True, 2 => False ).antipairs
If True was a subroutine instead of a term, you could append ()
sub True ( --> True ){}
True() => 1
Then there is using Pair.new.
Pair.new( True, 1 )

Using parens as in (True) => 0.3 or the null pseudo-package as in ::True => 0.3 would be another option.

Related

NEST (ElasticSearch) Terms Aggregation does not acknowledge the Query

I have a search request as below:
var response = client.Search<ProductElastic>(s => s
.Query(q => q
.Bool(b => b
.Should(mu => mu
.Match(m => m
.Field(f => f.title)
.Boost(1.5)
.Query(inputfilter.q)
), mu => mu
.Match(m => m
.Field(f => f.content)
.Query(inputfilter.q)
)
)
)
)
.Aggregations(a => a
.Terms("doctype_i", he => he
.Field(g => g.doctype_i)
)
.Terms("category_i", e => e
.Field(ge => ge.category_i)
.Size(100)
)
)
);
inputfilter.q holds the search term. When inputfilter.q is null it returns all the results and my aggregation bucket numbers are accurately represented. When inputfilter.q is defined (ex. searching for "test") it refines the results set, however, my aggregation buckets still give me the full numbers as before, as if nothing was searched for. For the record, doctype_i and category_i are integer type fields in Elastic.
How do I make my .Aggregations acknowledge the .Query so the aggregation buckets reflect numbers based on the results set?

invert needs a Pair?

The invert method for Lists should return the inverted sequence, or at least that's what the source seems to imply. However:
say (1,3,2).invert
fails with:
(exit code 1) Type check failed in invert; expected Pair but got Int (1)␤ in block <unit>
The documentation does not help, because it's LTA in this area (and missing for List). But the source code does not admit any ambiguous interpretation. Is there anything I'm missing here?
Perhaps you intended to use the reverse method. Well, if you want the result to be (2,3,1).
It looks like the documentation of List.invert is indeed missing. It is intended to work only on a List that consists of Pairs. I'll write that up now.
EDIT: doc added with https://github.com/perl6/doc/commit/0ee3245776
In Perl6, operators subroutines and methods have a singular purpose for a given name.
In the case of the infix + operator it is to add two numbers. So if it is given something that is not a number it tries to turn it into a number before adding.
In the case of the .invert method, its fundamental purpose is to invert a Pair object. That is swap the .key and the .value of a Pair.
So everywhere that .invert can be used, it does so in the way that is most like inverting a Pair object.
On a Pair object with a singular .value, it swaps the key and the value.
say ('a' => 'A').invert;
# (A => a)
If the .value is not singular it gives you a sequence where each value is now the key of its own Pair.
say ('a' => ('A', 'B')).invert;
# (A => a B => a)
Note that .invert always returns a sequence to be consistent. Even on that first example.
On a Hash it does it on all of the key / value pairs.
say %( 'a' => ('A','B'), 'b' => 'B', 1 => 'A' ).invert.sort;
# (A => 1 A => a B => a B => b)
On a List, it could do it one of two ways.
It could use the index as the key, exactly like .antipairs does.
say ( 'a' => ('A','B'), 'b' => 'B', 1 => 'A' ).antipairs.sort;
# ((1 => A) => 2 (a => (A B)) => 0 (b => B) => 1)
say ( 'a', 'b', 'c' ).antipairs;
# (a => 0 b => 1 c => 2)
It could go through each of the Pairs in the list like it currently does.
say ( 'a' => ('A','B'), 'b' => 'B', 1 => 'A' ).invert.sort
(A => 1 A => a B => a B => b)
Since .antipairs already works like .pairs except the opposite, there is really no reason for .invert to also work like it. That would have also made .invert less like a Pair method.
This also has the nice effect that you can get the Pairs from a Hash as a list then call .invert on it, and it will work like just calling .invert on the Hash directly.
say %( 'a' => ('A','B'), 'b' => 'B', 1 => 'A' ).invert.sort;
# (A => 1 A => a B => a B => b)
say %( 'a' => ('A','B'), 'b' => 'B', 1 => 'A' ).list.invert.sort;
# (A => 1 A => a B => a B => b)
It also means that you can call .invert several times and it stays consistent.
say %( 'a' => ('A','B'), 'b' => 'B', 1 => 'A' ).invert.invert.invert.sort;
# (A => 1 A => a B => a B => b)

how to get result for api connecting to whoapi?

As according to the mention documentation at whoapi.com i have gone through as mention according to instruction and tried implementing the api but the variable doesn't pass from one page to other.For example i have one page which have input type and submit button then when i click submit button it should redirect to who api url and display result
http://api.whoapi.com/?domain=whoapi.com&apikey=demokey&r=blacklist
whoapi.com = name of url,demokey = key assigned by who api & blacklist = name of service of who api
guide me through the connecting protocol.
Not sure I understand the question. Using the API should be straight simple as any REST request is.
PHP example:
$domain = "whoapi.com";
$r = "blacklist";
$apikey = "demokey";
$request = "http://api.whoapi.com/?domain=$domain&r=$r&apikey=$apikey";
$output = json_decode(file_get_contents($request), true);
Resulting array with print_r($output) looks like this:
[status] => 0
[ip] => 88.198.98.181
[blacklisted] => 0
[blacklists] => Array
(
[0] => Array
(
[tracker] => invaluement.com
[blacklisted] => 0
)
[1] => Array
(
[tracker] => surbl.org
[blacklisted] => 0
)
[2] => Array
(
[tracker] => barracudacentral.org
[blacklisted] => 0
)
[3] => Array
(
[tracker] => sorbs.net
[blacklisted] => 0
)
[4] => Array
(
[tracker] => spamhaus.org
[blacklisted] => 0
)
)
Hope this helps.

How to do multiple where statements for the same column in ActiveRecord?

Using rails 3, How could one do multiple where statements without complicated stuff or extra gems?
Im having this column "accepted" and would like to get all the values where accepted == false and accepted == null
Both of examples below fail:
#scholars = Scholars.where(:scholar_id => current_user.id).where(["accepted = ? or accepted = ?", true, null])
and
#scholars = Scholars.where(:scholar_id => current_user.id).where(:accpeted => true).where(:accepted=> null)
Try:
#scholars = Scholars.where(:scholar => current_user, :accepted => true).all +
Scholar.where(:scholar => current_user, :accepted => nil).all
Did you name your model "Scholars"? Models are traditionally singular... if you named it properly, this should be Scholar.where(...).
The correct answer should be
#profiles = Profile.where(:user_id => current_user.id, :accepted => [true, nil]).order(:accepted)

Optimise ignoring of undefined variables when building find conditions in Rails

I have a an method that retrieves Groups that are present in certain areas. Groups are given a country_id, region_id and city_id
The UI gives three select boxes to choose a country, a region from that country and then a city from that region. To find all groups in a particular city, I have this code:
#groups = Group.find(:all, :conditions => {:city_id => params[:city_id]})
This all works fine, but I also want it to find all groups in an area when the lower criteria isn't specified. For example, If a country and region are given, but not city, I'd like to find it by the region.
What I'm doing is this:
if !params[:city_id].nil?
#groups = Group.find(:all, :conditions => {:city_id => params[:city_id]})
else
if !params[:region_id].nil?
#groups = Group.find(:all, :conditions => {:region_id => params[:region_id]})
else
#groups = Group.find(:all, :conditions => {:country_id => params[:country_id]})
end
end
This works perfectly well, but it seems like it's a little inefficient. Am I doing it the best way or can I streamline a little?
One idea I had was to have a single find checking against all parameters, but I could not work out how to effectively 'ignore' parameters that were nil - my main thought was to check which ones were not set and set them to something like '*' or 'true', but that's not how SQL plays the game.
Sounds like a job for named scopes:
class Group < ActiveRecord::Base
named_scope :in_city, lambda { |city_id| {
:conditions => { :city_id => city_id }
}}
named_scope :in_region, lambda { |region_id | {
:conditions => { :region_id => region_id }
}}
named_scope :in_country, lambda { |country_id | {
:conditions => { :country_id => country_id }
}}
end
This establishes some simple scopes for restricting the Group records. Presumably you have indexed your database properly so these are quick to resolve.
The controller is much easier to implement then:
def index
#group_scope = Group
if (!params[:city_id].blank?)
#group_scope = #group_scope.in_city(params[:city_id])
elsif (!params[:region_id].blank?)
#group_scope = #group_scope.in_region(params[:region_id])
elsif (!params[:country_id].blank?)
#group_scope = #group_scope.in_country(params[:country_id])
end
#groups = #group_scope.all
end
Generally you should be testing for .blank? instead of .nil? as some form elements can send in empty results, such as a select with something akin to "All" as the default.
You could use some Ruby idioms to get something a little more succinct.
Try something like this: (untested code!)
def index
#groups = Group.find :all, :conditions => [:city_id, :region_id, :country_id].inject {} do |conditions, name|
conditions[name] = params[name] unless params[name].blank?
conditions
end
end
If every value in params is a candidate for :conditions you can just do this:
#groups = Group.all(:conditions => params.reject { |idx, val| val.nil? })
This just throws out nil values from params and uses the remaining values for conditions.
If you don't want to use all of the values in params, you have two options. You can just get rid of a bunch of redundancy in your original code:
conditions = if !params[:city_id].nil?
{ :city_id => params[:city_id] }
elsif !params[:region_id].nil?
{ :region_id => params[:region_id] }
else
{ :country_id => params[:country_id] }
end
#groups = Group.all(:conditions => conditions)
You can knock of a few more lines like this, but it sacrifices a bit of readability IMO:
conditions = if !params[:city_id].nil? then { :city_id => params[:city_id] }
elsif !params[:region_id].nil? then { :region_id => params[:region_id] }
else { :country_id => params[:country_id] }
end
Or you can do something like this:
conditions = [:city_id, :region_id, :country_id].inject({}) do |hsh, sym|
hsh[sym] = params[sym] unless params[sym].nil?
hsh
end
#groups = Group.all(:conditions => conditions)
This has the advantage that you don't need to add another condition for each symbol.