Skip to content

Instantly share code, notes, and snippets.

@vasily-kirichenko
vasily-kirichenko / gist:3878993
Created October 12, 2012 12:33
Clojure Fibonacci
(defn fib [n]
(if (< n 2)
n
(+ (fib (- n 1))
(fib (- n 2)))))
(time
(doseq [n (range 40)]
(prn (format "Fib(%s) = %s" n (fib n)))))
@vasily-kirichenko
vasily-kirichenko / gist:3945511
Created October 24, 2012 11:12
amazing c# generics
public static IProcessorWithContext<TInputData, TOutputData, TContext> Combine
<TInputData, TIntermediateData, TOutputData, TContext>(
this IProcessorWithContext<TInputData, TIntermediateData, TContext> first,
IProcessorWithContext<TIntermediateData, TOutputData, TContext> second)
{
return new ProcessorWithContextCombinator<TInputData, TIntermediateData, TOutputData, TContext>(first, second);
}
@vasily-kirichenko
vasily-kirichenko / gist:3992893
Created November 1, 2012 10:13
Multimethods vs Protocols
(defrecord Order [price])
;; protocol
(defprotocol Calculable
(p-total [this]))
(extend-protocol Calculable
Order
(p-total [order] (:price order))
@vasily-kirichenko
vasily-kirichenko / gist:3992904
Created November 1, 2012 10:16
mm vs protocols results
created 20000000 orders.
Elapsed time: 363.78375 msecs
protocol done.
Elapsed time: 1280.251512 msecs
multimethod done.
(defmacro >> [& [first-expr & rest-exprs]]
(if (empty? rest-exprs)
first-expr
`(let [~'it ~first-expr]
(thread-it ~@rest-exprs))))
(>>
[jay john mike chris]
(filter (comp (partial = "new york") :current-city) it)
(group-by :employer it)
@vasily-kirichenko
vasily-kirichenko / gist:4026186
Created November 6, 2012 17:27
Thread-it data
(defmacro thread-it [& [first-expr & rest-exprs]]
(if (empty? rest-exprs)
first-expr
`(let [~'it ~first-expr]
(thread-it ~@rest-exprs))))
(def jay {:name "jay fields" :employer "drw.com" :current-city "new york"})
(def john {:name "john dydo" :employer "drw.com" :current-city "new york"})
(def mike {:name "mike ward" :employer "drw.com" :current-city "chicago"})
(def chris {:name "chris george" :employer "thoughtworks.com" :current-city "new york"})
@vasily-kirichenko
vasily-kirichenko / gist:4066913
Created November 13, 2012 16:49
F# maybe monad
type Maybe() =
member __.Bind(value, rest) =
if value = null then null else rest value
member __.Return(value) =
value
let maybe = new Maybe()
let substring (str: string) (from: int) =
type StatefulFunc<'state, 'result> = StatefulFunc of ('state -> 'result * 'state)
let Run (StatefulFunc f) initialState = f initialState
// what the hell (StatefulFunc f) means here?..
@vasily-kirichenko
vasily-kirichenko / gist:4103995
Last active October 12, 2015 23:28
MakeChange
let makeChange amount =
let rec doChange amount soFar coins =
match coins with
|coin :: _ when amount >= coin ->
doChange (amount - coin) (coin :: soFar) coins
|_ :: rest ->
doChange amount soFar rest
|[] -> soFar
doChange amount [] [25; 10; 5; 1]
module FoqTest
open System
open Xunit
open Swensen.Unquote
open Foq
type ILogger = abstract Log: exn -> unit
type IService = abstract Translate: string -> string
type Translator(logger: ILogger, service: IService) =