How to pluck by key name matching? - rethinkdb

Say I have the following table:
[
{numberOfRedStripes: 7, numberOfBlueStripes: 6, stars: 50, foo: "bar"},
{numberOfRedStripes: 1, numberOfBlueStripes: 1, stars: 0, something: "else"}
]
How can I use regex in order to pluck only the docs which their KEYS start with the string 'numberOf', so that the result would be:
[
{numberOfRedStripes: 7, numberOfBlueStripes: 6},
{numberOfRedStripes: 1, numberOfBlueStripes: 1}
]
?

Does this work?
table.map {|row|
row.pluck(r.args(row.keys().filter{|key| key.match("^numberOf")}))
}

Related

Merge 2 maps at specific index

I was wondering if I can merge 2 sass maps at a specific index of the first map.
$numbers: (
"one": 1,
"two": 2,
"three": 3,
"six": 6,
"seven": 7,
);
$more-numbers: (
"four": 4,
"five": 5,
);
#debug map.merge($numbers, $more-numbers);
// ("one": 1, "two": 2, "three": 3, "six": 6, "seven": 7, "four": 4, "five": 5)
Instead, I want:
("one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6, "seven": 7)
I tried with the list module functions list.index() list.set-nth() but it replaces the index
$index: list.index($numbers, (three 3));
#debug list.set-nth($numbers, $index, $more-numbers);
// "one" 1, "two" 2, ("four": 4, "five": 5), "six" 6, "seven" 7
Even if that worked I am not sure how I can convert it back to a map.
Edit because someone asked the reason behind my question..
I am experimenting with generating utility classes.
u-padding-1: { padding: 0.375rem; }
I have already a map with the values:
$padding: (
"properties": (
"-padding": padding,
),
"directions": (
null: null,
"-horizontal": "-left" "-right",
"-vertical": "-top" "-bottom",
"-top": "-top",
"-right": "-right",
"-bottom": "-bottom",
"-left": "-left",
),
"values": (
"-none": 0rem,
"-tiny": t.rem(t.spacing(tiny)),
"-small": t.rem(t.spacing(small)),
null: t.rem(t.spacing(base)),
"-large": t.rem(t.spacing(large)),
"-huge": t.rem(t.spacing(huge)),
"-auto": auto,
),
) !default;
Now instead of manually adding all the utility key value pairs in the $padding->"values"
"-0": 0,
"-1": 1,
...
I generate them with a while loop and merge them. That would not be an issue but I need these utility classes to be added just before the keyword values like auto, full, etc. for cascading reasons.
I don't want to add another key value pair to my map, for example
"keywords: (
"-auto": auto,
"-full": 100%,
....
),
because I also generate responsive classes and it gets messy.
This is an example of a partial file.
#use "sass:map";
#use "sass:math";
#use "../settings/index.settings" as s;
#use "../tools/index.tools" as t;
$margin: (
"properties": (
"-margin": margin,
),
"directions": (
null: null,
"-horizontal": "-left" "-right",
"-vertical": "-top" "-bottom",
"-top": "-top",
"-right": "-right",
"-bottom": "-bottom",
"-left": "-left",
),
"values": (
"-none": 0px,
"-tiny": t.rem(t.spacing(tiny)),
"-small": t.rem(t.spacing(small)),
null: t.rem(t.spacing(base)),
"-large": t.rem(t.spacing(large)),
"-huge": t.rem(t.spacing(huge)),
"-auto": auto,
),
) !default;
// Populate map with values
$margin-arithmetic-progress: ();
$m: 0;
#while $m <= (t.strip-unit(s.$global-baseline) * 5) {
$converted: t.rem($m);
$margin-arithmetic-progress: map.set($margin-arithmetic-progress, "values", "-#{math.div($m, t.strip-unit(s.$global-baseline))}", $converted);
$m: $m + t.strip-unit(s.$global-baseline);
}
$margin: map.deep-merge($margin, $margin-arithmetic-progress);
#include t.generate-classes($margin);
#include t.generate-responsive-classes($margin);

How to custom OrderBy in Laravel

I wanted to do a custom ordering for marketing_status_id in this:
$prospect = $this->filterProspect($request->all())
->select([
'student_marketings.*',
'students.name',
'students.identification_type_id',
])
->distinct('student_marketings.id')
->orderBy('student_marketings.marketing_status_id');
How to order it in this sequence: ('marketing_status_id', 5, 20, 4, 1, 3, 18, 6) ?
Use mysql inbuilt-method field() like this:
$prospect = $this->filterProspect($request->all())
->select([
'student_marketings.*',
'students.name',
'students.identification_type_id',
])
->distinct('student_marketings.id')
->orderBy(DB::raw('FIELD(marketing_status_id, 5, 20, 4, 1, 3, 18, 6)'));

Overriding a hash in ruby

I am very new to ruby and I am trying to find if there's an equivalent way to doing the thing in ruby.
In yml syntax, we use a syntax similar to this way to have a default blob and then override them with specific values:
default:
default:
A: {read: 20, write: 10}
B: {read: 30, write: 30}
C: {read: 130, write: 10}
override1:
placeholderA:
A: {read: 10, write: 10}
override2:
placeHolderB:
A: {read: 10, write: 10}
B: {read: 5, write: 5}
C: {read: 5, write: 5}
D: {read: 5, write: 5}
I wanted to know if we can create a hash in ruby where in it will pick the values for the override if they exist, otherwise it will just pick the default value.
I am not sure if ruby merge map is an approach to this problem (since I am still new to ruby, I am exploring options).
Is this possible?
merge could be used:
options = {a:22}
my_defaults = {a:1, b:123}
my_defaults.merge(options)
> {a:22, b:123}
if you are using rails that also provides a reverse_merge which works the other way round and may be clearer to read intent from in some use cases
options = { a:2, b:321 }
my_defaults = {a:1, c:3}
options.reverse_merge(my_defaults)
> {a:2, b:321, c:3}
http://apidock.com/rails/Hash/reverse_merge

What is the syntax for constructing a list-value in ATS?

For instance, how can I construct a list consisting of all the digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
You can construct it with val xs = ($list {int} (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)).
Make sure you specify the correct memory allocation functions by passing -DATS_MEMALLOC_LIBC to the compiler when using this code.
If you compile to JavaScript via atscc2js, then you need to construct
the list in the following manner:
val ds =
0::1::2::3::4::5::6::7::8::9::nil{int}()
// end of [val]
It also works for targeting C.
There are also combinators for this sort of things. For instance,
val ds = (10).list_map(TYPE{int})(lam(i) => i)
val ds = list_tabulate_cloref<int>(10, lam i => i)
You can find special literal on emacs mode:
https://github.com/githwxi/ATS-Postiats/blob/653af81715cf6bfbd1d2cd5ece1e88c8c3912b4a/utils/emacs/ats2-mode.el#L287
(defvar ats-special-keywords
'("$arrpsz" "$arrptrsize" "$delay" "$ldelay" "$effmask" "$effmask_ntm" "$effmask_exn" "$effmask_ref"
"$effmask_wrt" "$effmask_all" "$extern" "$extkind" "$extype" "$extype_struct" "$extval" "$lst"
"$lst_t" "$lst_vt" "$list" "$list_t" "$list_vt" "$rec" "$rec_t" "$rec_vt"
"$record" "$record_t" "$record_vt" "$tup" "$tup_t" "$tup_vt" "$tuple" "$tuple_t"
"$tuple_vt" "$raise" "$showtype" "$myfilename" "$mylocation" "$myfunction" "#assert" "#define"
"#elif" "#elifdef" "#elifndef" "#else" "#endif" "#error" "#if" "#ifdef"
"#ifndef" "#print" "#then" "#undef" "#include" "#staload" "#dynload" "#require"))
If you find some keyword, you can easily know how to use it on doc/EXAMPLE/ directory:
$ git clone https://github.com/githwxi/ATS-Postiats.git
$ cd ATS-Postiats/doc/EXAMPLE
$ grep -r "\$list" . | head
./MISC/word-chain.dats: $list{word}("", "")
./MISC/word-chain.dats: $list{word}("", "")
./MISC/mysendmailist.dats:$list{string}
./MISC/monad_list.dats: $list{a}("this", "that", "a")
./MISC/monad_list.dats: $list{a}("frog", "elephant", "thing")
./MISC/monad_list.dats: $list{a}("walked", "treaded", "grows")
./MISC/monad_list.dats: $list{a}("slowly", "quickly")
./ATSLF/CoYonedaLemma.dats:val myintlist0 = g0ofg1($list{int0}(I(1), I(0), I(1), I(0), I(0)))
./ATSLF/YonedaLemma.dats: $list{bool}(True, False, True, False, False)
./ATS-QA-LIST/qa-list-2014-12-07.dats:$list{double}(0.111111, 0.222222, 0.333333)
For a list0-value, you can do
val xs = g0ofg1($list{T}(x1, ..., xn))
where T is the type for the elements in xs. For instance,
val some_int_list = g0ofg1($list{int}(0, 9, 8, 7, 3, 4))

Getting max and min from two different sets in json

I haven't found a solution with data set up quite like mine...
var marketshare = [
{"store": "store1", "share": "5.3%", "q1count": 2, "q2count": 4, "q3count": 0},
{"store": "store2","share": "1.9%", "q1count": 5, "q2count": 10, "q3count": 0},
{"store": "store3", "share": "2.5%", "q1count": 3, "q2count": 6, "q3count": 0}
];
Code so far, returning undefined...
var minDataPoint = d3.min( d3.values(marketshare.q1count) ); //Expecting 2 from store 1
var maxDataPoint = d3.max( d3.values(marketshare.q2count) ); //Expecting 10 from store 2
I'm a little overwhelmed by d3.keys, d3.values, d3.maps, converting to array, etc. Any explanations or nudges would be appreciated.
I think you're looking for something like this instead:
d3.min(marketshare, function(d){ return d.q1count; }) // => 2.
You can pass an accessor function as the second argument to d3.min/d3.max.

Resources