Skip to content

Instantly share code, notes, and snippets.

@nathanmarz
nathanmarz / gist:7a6bbf80770d2a232f33
Created June 24, 2015 17:11
Practical example of Specter
(def world
{:people [{:money 129827 :name "Alice Brown"}
{:money 100 :name "John Smith"}
{:money 6821212339 :name "Donald Trump"}
{:money 2870 :name "Charlie Johnson"}
{:money 8273821 :name "Charlie Rose"}
]
:bank {:funds 4782328748273}}
)
@nathanmarz
nathanmarz / gist:6333a50c67ec9031e5ee
Last active August 29, 2015 14:23
New if-path selector in Specter
;; For every map, if value for :a is even, increment :b's value. Otherwise, increment all values.
(update [ALL (if-path [:a even?] :b [ALL LAST])]
inc
[{:a 2 :b 3 :c 4}
{:a 1 :b 10 :d 5 :e 6}
{:a 2 :b 0}
{:a -1 :f 100}])
==>
[{:c 4, :b 4, :a 2}
@nathanmarz
nathanmarz / gist:7fb2ef8af5c2293b56ee
Created June 27, 2014 05:49
clojure monad parser
(use 'clojure.algo.monads)
(def parser-m (state-t maybe-m))
(defmacro parserfn [& body]
`(domonad parser-m ~@body))
(with-monad parser-m
(defn optional [p] (m-plus p (m-result nil)))
isolation.scheduler.machines:
"my-topology": 8
"tiny-topology": 1
"some-other-topology": 3
@nathanmarz
nathanmarz / gist:3234661
Created August 2, 2012 07:03
Grouped aggregation example
stream.groupBy(new Fields("val1"))
.aggregate(new Fields("val2"), new Sum(), new Fields("sum"))
@nathanmarz
nathanmarz / gist:3234656
Created August 2, 2012 07:03
Aggregate example
stream.aggregate(new Fields("val2"), new Sum(), new Fields("sum"))
@nathanmarz
nathanmarz / gist:3234653
Created August 2, 2012 07:02
Function example
stream.each(new Fields("x", "y"), new AddAndMultiply(), new Fields("added", "multiplied"));
@nathanmarz
nathanmarz / gist:3234652
Created August 2, 2012 07:02
Add and multiply function
public class AddAndMultiply extends BaseFunction {
public void execute(TridentTuple tuple, TridentCollector collector) {
int i1 = tuple.getInteger(0);
int i2 = tuple.getInteger(1);
collector.emit(new Values(i1 + i2, i1 * i2));
}
}
@nathanmarz
nathanmarz / gist:3234650
Created August 2, 2012 07:01
Filter example implementation
public class MyFilter extends BaseFilter {
public boolean isKeep(TridentTuple tuple) {
return tuple.getInteger(0) < 10;
}
}
@nathanmarz
nathanmarz / gist:3234647
Created August 2, 2012 07:01
Filter example
stream.each(new Fields("y"), new MyFilter())