How to create custom operator from a pipe of operators in IxJS? - ixjs

In rxjs6, we can create an operator from a pipe of operators.
import { pipe } from 'rxjs';
function doSomething() {
return pipe(
map(...),
flatMap(...),
);
}
$.pipe(
map(...),
doSomething(),
flatMap(...),
)
Is there a way to create an operator like this in IxJS?

You can combine operators manually:
import { IterableX as Iterable } from 'ix/iterable';
import { map, filter } from 'ix/iterable/pipe/index';
function customOperator() {
return source$ => map(x => x * x)(
filter(x => x % 2 === 0)
(source$)
);
}
const results = Iterable.of(1, 2, 3, 4).pipe(
customOperator()
).forEach(x => console.log(`Next ${x}`));
Or write your own pipe implementation:
const pipe = (...fns) =>
source$ => fns.reduce(
(acc, fn) => fn(acc),
source$
);
function customOperator() {
return pipe(
filter(x => x % 2 === 0),
map(x => x * x)
)
}

Related

Slick Queries With GroupBy

I'm new to Scala and Slick. I'm trying to run a query with a grouping by an object and creating two sequences for each object.
This is my code :
val firstQuery = {
for {
table1Id <- Table1.filter(_.someid === someid).map(_.someid)
table2 <- Table2.filter(_.someid === table1Id)
table3 <- Table3.filter(_.someId === someid)
_ <- {
Table4.filter(table4 => table4.someid === table3.someid &&
table4.name === "$$$")
}
table5 <- Table5.filter(_.someid === table2.someid)
} yield {
(table2, table3, table5)
}
}
val finalQuery = {
for {
(table6, firstQueryTuple) <- {
Table6 joinRight firstQuery on
((firstTable, secondTable) => firstTable.someid === secondTable._1.someid)
}
} yield {
(table6, firstQueryTuple._1, firstQueryTuple._2, firstQueryTuple._3)
}
}
dataBaseConnection.run(finalQuery.result).map { resultSequence =>
val grouping = resultSequence.groupBy { case (table6, table2, _, _) => (table6, table2) }
val groupingFiltered = grouping.map { case (table6table2tuple, sequence) => (table6table2tuple, sequence.map(_._4) (which is Table5), sequence.map(_._3) (which is table3)) }.toSeq
groupingFiltered match {
case tupleQuery if tupleQuery.nonEmpty => Success(tupleQuery.map(x => (x._1._2, x._1._1, x._2, x._3) ))
case _ => Failure(NonEmptyList(DBMissingRecordProblem(s"Error")))
}
}
Now what I need to achieve is this result
Seq[(Table2, Option[Table6], Seq[Table5], Seq[Table3])]
Is there a way to group by and achieve two sequences? I know it can be done for one sequence, I mean group by an object and achieve (object, Seq[anotherObject])
Please help me if you can.

How to write a macro in Rust to match any element in a set?

In C, I'm used to having:
if (ELEM(value, a, b, c)) { ... }
which is a macro with a variable number of arguments to avoid typing out
if (value == a || value == b || value == c) { ... }
A C example can be seen in Varargs `ELEM` macro for use with C.
Is this possible in Rust? I assume it would use match. If so, how would variadic arguments be used to achieve this?
macro_rules! cmp {
// Hack for Rust v1.11 and prior.
(#as_expr $e:expr) => { $e };
($lhs:expr, $cmp:tt any $($rhss:expr),*) => {
// We do this to bind `$lhs` to a name so we don't evaluate it multiple
// times. Use a leading underscore to avoid an unused variable warning
// in the degenerate case of no `rhs`s.
match $lhs { _lhs => {
false || $(
cmp!(#as_expr _lhs $cmp $rhss)
) || *
// ^- this is used as a *separator* between terms
}}
};
// Same, but for "all".
($lhs:expr, $cmp:tt all $($rhss:expr),*) => {
match $lhs { _lhs => {
true && $( cmp!(#as_expr _lhs $cmp $rhss) ) && *
}}
};
}
fn main() {
let value = 2;
if cmp!(value, == any 1, 2, 3) {
println!("true! value: {:?}", value);
}
if cmp!(value*2, != all 5, 7, 1<<7 - 1) {
println!("true! value: {:?}", value);
}
}
First off, if your a, b, and c are concrete values, you can just use match:
fn main() {
let x = 42;
match x {
1 | 2 | 3 => println!("foo"),
42 => println!("bar"),
_ => println!("nope"),
}
}
If you want to match on variables you need to write the match arms like this:
match x {
x if x == a || x == b || x == c => println!("foo"),
42 => println!("bar"),
_ => println!("nope"),
}
…which is basically what you want to avoid.
But: A pretty direct translation of your C macro is also possible!
macro_rules! elem {
($val:expr, $($var:expr),*) => {
$($val == $var)||*
}
}
fn main() {
let y = 42;
let x = 42;
if elem!(x, 1, 3, y) {
println!("{}", x);
}
}
I'm partial to writing this without a macro, taking advantage of contains on arrays.
fn main() {
if [1, 2, 3, 4].contains(&4) {
println!("OK");
}
}
It's hard to predict what will happen to this when optimized, but if absolute performance is a goal you'd do well to benchmark each approach.
Yes this is possible, the following macro expands to do each check.
macro_rules! elem {
($n:expr, $( $hs:expr ),*) => ($( $n == $hs )||* );
}
fn main() {
if elem!(4, 1, 2, 3, 4) {
println!("OK");
}
}
Thanks to #vfs on #rust in IRC.

scopt3 sample script does not compile

I'm trying to use scopt3 in my project but I get compilation errors even for the sample code on scopt3 Github page:
val parser = new scopt.OptionParser[Config]("scopt") {
head("scopt", "3.x")
opt[Int]('f', "foo") action { (x, c) =>
c.copy(foo = x) } text("foo is an integer property")
opt[File]('o', "out") required() valueName("<file>") action { (x, c) =>
c.copy(out = x) } text("out is a required file property")
opt[(String, Int)]("max") action { case ((k, v), c) =>
c.copy(libName = k, maxCount = v) } validate { x =>
if (x._2 > 0) success else failure("Value <max> must be >0")
} keyValueName("<libname>", "<max>") text("maximum count for <libname>")
opt[Seq[File]]('j', "jars") valueName("<jar1>,<jar2>...") action { (x,c) =>
c.copy(jars = x) } text("jars to include")
opt[Map[String,String]]("kwargs") valueName("k1=v1,k2=v2...") action { (x, c) =>
c.copy(kwargs = x) } text("other arguments")
opt[Unit]("verbose") action { (_, c) =>
c.copy(verbose = true) } text("verbose is a flag")
opt[Unit]("debug") hidden() action { (_, c) =>
c.copy(debug = true) } text("this option is hidden in the usage text")
note("some notes.\n")
help("help") text("prints this usage text")
arg[File]("<file>...") unbounded() optional() action { (x, c) =>
c.copy(files = c.files :+ x) } text("optional unbounded args")
cmd("update") action { (_, c) =>
c.copy(mode = "update") } text("update is a command.") children(
opt[Unit]("not-keepalive") abbr("nk") action { (_, c) =>
c.copy(keepalive = false) } text("disable keepalive"),
opt[Boolean]("xyz") action { (x, c) =>
c.copy(xyz = x) } text("xyz is a boolean property"),
checkConfig { c =>
if (c.keepalive && c.xyz) failure("xyz cannot keep alive") else success }
)
}
errors are:
1) not found type Config
I thougth it was com.typesafe.config.Config, but when I import I get "velue copy is not a member of com.typesafe.config.Config". Where does Config come from?
2) not found value foo
All arguments to .copy() method are marked as "not found values" (I suppose is due to the previous error on Config)
I'm on scala 2.11.6 / SBT 0.13.8
Any help?
scopt has nothing to do with typesafe-config at all.
If you carefully read the README, you will notice that Config is defined right before the code you paste here:
case class Config(foo: Int = -1, out: File = new File("."), xyz: Boolean = false,
libName: String = "", maxCount: Int = -1, verbose: Boolean = false, debug: Boolean = false,
mode: String = "", files: Seq[File] = Seq(), keepalive: Boolean = false,
jars: Seq[File] = Seq(), kwargs: Map[String,String] = Map())
It is merely an example of an object that holds all parameters of a program.
The idea is you parametrise OptionParser with any configuration object you wish. E.g.
case class Foo(name: String)
val parser = new scopt.OptionParser[Foo]("foo") {
opt[String]('n', "name") action { (n, foo) => foo.copy(name = n) }
}
val res = parser.parse(args, Foo("default"))

Value * is not a member of AnyVal

This is a fold that I wrote and I get this error:
Error:(26, 42) value * is not a member of AnyVal
(candE.intersect(candR), massE * massR)
^
allAssignmentsTable is a List[Map[Set[Candidate[A]],Double]]
val allAssignmentsTable = hypothesis.map(h => {
allAssignments.map(copySet => {
if(h.getAssignment.keySet.contains(copySet))
(copySet -> h.getAssignment(copySet))
else
(copySet -> 0.0)
}).toMap
})
val aggregated = allAssignmentsTable.foldLeft(initialFold) { (res,element) =>
val allIntersects = element.map {
case (candE, massE) =>
res.map {
case (candR, massR) => candE.intersect(candR), massE * massR
}.toList
}.toList.flatten
val normalizer = allIntersects.groupBy(_._1).filter(_._1.size == 0).map {
case(key, value) => value.foldLeft(0.0)((e,i) => i._2 + e)
}.head
allIntersects.groupBy(_._1).map {
case(key, value) => key -> value.foldLeft(0.0)((e,i) => i._2 + e)
}
}
if I do this: case(candE, massE:Double) then I won't get an error but I will get exception in match.
The problem that you get here:
val aggregated = allAssignmentsTable.foldLeft(initialFold) { (res,element) =>
val allIntersects = element.map {
case (candE, massE) =>
res.map {
case (candR, massR) => candE.intersect(candR), massE * massR
}.toList
}.toList.flatten
is most probably arising from the previous code block:
val allAssignmentsTable = hypothesis.map(h => {
allAssignments.map(copySet => {
if(h.getAssignment.keySet.contains(copySet))
(copySet -> h.getAssignment(copySet))
else
(copySet -> 0.0)
}).toMap
})
My hypothesis is that h.getAssignment(copySet) returns something else instead of Double (which seems to be confirmed by the error message quoted in the OP - (26, 42)etc, neither of these two values look like it is a Double. Therefore, allAssignmentsTable undercover is probably not List[Map[Set[Candidate[A]],Double]] but something else e.g. it has Any instead of Double, therefore operator * cannot be applied.

How to use variadic macros to call nested constructors?

I'm trying to create a macro in Rust that lets me write
make_list!(1, 2, 3)
instead of
Node::new(1, Node::new(2, Node::new(3, None)))
which should work for an arbitrary number of "parameters" including zero. This is what I have so far:
macro_rules! make_list(
() => (
None
);
( $x:expr, $( $more:expr ),* ) => (
Node::new($x, make_list!( $( $more ),* ))
)
);
but I get the following error:
error: unexpected end of macro invocation
--> src/main.rs:19:42
|
19 | Node::new($x, make_list!( $( $more ),* ))
| ^^^^^
I can't make much sense of this. From what I can tell, it should work. What did I do wrong?
The complete code:
type List<T> = Option<Box<Node<T>>>;
struct Node<T> {
value: T,
tail: List<T>,
}
impl<T> Node<T> {
fn new(val: T, tai: List<T>) -> List<T> {
Some(Box::new(Node::<T> {
value: val,
tail: tai,
}))
}
}
macro_rules! make_list(
() => (
None
);
( $x:expr, $( $more:expr ),* ) => (
Node::new($x, make_list!( $( $more ),* ))
)
);
fn main() {
let _list: List<i32> = make_list!(1, 2, 3, 4, 5, 6, 7, 8, 9);
}
Expanding on the error: you get down to the case where there is only one value, and so it writes make_list!(1). However, there is no rule that will match that, for the second rule, after consuming the expression x, wants a comma, which is not provided.
So you need to make it so that it will work for make_list!(1) and not just (in fact, just not) make_list!(1,). To achieve this, get the comma inside the repeating part, like this:
macro_rules! make_list(
() => (
None
);
( $x:expr $( , $more:expr )* ) => (
Node::new($x, make_list!( $( $more ),* ))
)
);
Bonus: you can write make_list![1, 2, 3] instead of make_list!(1, 2, 3) if you want.
As noted by #chris-morgan's answer, expanding the single argument case isn't accounted for.
So you can either include comma in the expansion, or add a single case in the macro:
Example of both, single argument:
macro_rules! make_list {
() => (
None
);
($x:expr) => (
Node::new($x, None)
);
($x:expr, $($more:expr),+) => (
Node::new($x, make_list!($($more),*))
);
}
Including the comma in the expansion:
macro_rules! make_list {
() => (
None
);
($x:expr $(, $more:expr)*) => (
Node::new($x, make_list!($($more),*))
);
}
Here is a fully working example based on the question and updated for Rust 1.14:
type List<T> = Option<Box<Node<T>>>;
#[derive(Debug)]
struct Node<T> {
value: T,
tail: List<T>
}
impl<T> Node<T> {
fn new(val: T, tai: List<T>) -> List<T> {
Some(Box::new(Node::<T> { value: val, tail: tai }))
}
}
macro_rules! make_list {
() => (
None
);
($x:expr $(, $more:expr)*) => (
Node::new($x, make_list!($($more),*))
);
}
fn main() {
let list: List<i64> = make_list!();
println!("{:?}", list);
let list: List<i64> = make_list!(1);
println!("{:?}", list);
let list: List<i64> = make_list!(1,2,3,4,5,6,7,8,9);
println!("{:?}", list);
}