Skip to content

Instantly share code, notes, and snippets.

View psfblair's full-sized avatar

Paul Blair psfblair

  • Serious Code
  • New York, NY
View GitHub Profile
Import Directory
Import Store
-- MODEL
{- Each new workflow requires:
1. A new case of Workflow
2. A new field in the model holding the workflow's state
3. An init value of that field in init (not shown; it's uninteresting)
-}
@psfblair
psfblair / gist:25e5e96a653cc7394941
Last active August 29, 2015 14:04
Emulating higher-kinded types in F#, maybe...?
type Functor<'A, 'T> = abstract member Map : ('T -> 'U) -> Functor<'A, 'U>
type ListFunctor<'T>(wrappedList: list<'T>) =
member this.Wrapped = wrappedList
interface Functor<list<obj>, 'T> with
member this.Map (mappingFunction: 'T -> 'U) : Functor<list<obj>, 'U> =
let result = List.map mappingFunction wrappedList
new ListFunctor<'U>(result) :> Functor<list<obj>, 'U>
(* Recursive way - this gives the nth fibonacci number *)
let rec fib n = if n < 2 then 1 else fib (n - 1) + fib (n - 2)
let fibonaccis = 0|> Seq.unfold (fun(x) -> Some(fib(x), x+1))
Seq.takeWhile (fun x -> x < 4000000) fibonaccis
|> Seq.filter (fun x -> x % 2 = 0)
|> Seq.fold (+) 0
(* Try putting the upper sequence limit into the sequence
Something's wrong here: takes five minutes and the answer is wrong (1485607536) *)
let rec fib n = if n < 2 then 1 else fib (n - 1) + fib (n - 2)
; Here's one using the standard fibonacci sequence to get the nth fibonacci number
; The nth fibonacci is the sum of the n-1 and the n-2 fibonacci, unless n is < 2 in which case it is 1
(defn fibs [x]
(defn inner-fib [x one-back two-back]
(case x
0 two-back
1 one-back
(recur (dec x) (+ one-back two-back) one-back)
)
)
(* Each new term in the Fibonacci sequence is generated by adding the previous two terms.
By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Find the sum of all the even-valued terms in the sequence which do not exceed four million. *)
#light
let fibonacci = fun(x: int, y: int) -> Some( x, (y, x+y) )
let sequence = Seq.unfold fibonacci <| (1,2) //I like having the tuple at the end better than at the beginning.
#light
(* If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000. *)
(* Easiest way: Use a set *)
let multiples = new Set<int>([0..3..1000] @ [0..5..1000])
let answer = Set.fold (+) 0 multiples
/* If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
*/
// List comprehension
object ex1_a {
val multiples = for (i <- List.range(1, 1000) if i % 3 == 0 || i % 5 == 0 ) yield i;
val answer = multiples.foldLeft (0) ((x,y) => x + y);
}
; If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
; The sum of these multiples is 23.
; Find the sum of all the multiples of 3 or 5 below 1000.
; The simple version -- didn't know that ranges had a start/end/step version
(apply +
(for [multiple (range 1000)
:when (or (= 0 (mod multiple 3))
(= 0 (mod multiple 5)))]
-- Alex's simple Haskell version
foldl (+) 0 [x | x <- [1..999], x `mod` 3 == 0 || x `mod` 5 == 0]
-- List comprehensions, naturally lazy
let multipleOf3Or5 x = (mod x 3 == 0) || (mod x 5 == 0)
foldl (+) 0 $ takeWhile (<1000) [x | x <- [1..], multipleOf3Or5 x]
-- Sets
-- Prelude> :m + Data.Set
let multiplesOf3 = Data.Set.fromList [0,3..1000]
def distance(*args)
flattened = args.flatten
if flattened.length == 3
hash_arg = flattened.pop
raise ArgumentError unless hash_arg.respond_to?(:each_key)
flattened << hash_arg[:to][0]
flattened << hash_arg[:to][1]
end