Skip to content

Instantly share code, notes, and snippets.

val fibs: Stream[Int] = 0 #:: fibs.scanLeft(1)(_ + _)
fibs.take(20).toList
fibs take 20 toList
val fibs: Stream[BigInt] = 0 #:: fibs.scanLeft(1)(_ + _)
@nickleeh
nickleeh / sampleNumber.fs
Created May 15, 2015 08:32
Returns the first n items from a sequence in F#.
// Sequence of random numbers
open System
let randomSequence =
seq {
let rng = new Random()
while true do
yield rng.Next()
}
@nickleeh
nickleeh / memoization.fs
Created May 13, 2015 12:37
Memoizing pure functions
open System.Collections.Generic
let memoize (f : 'a -> 'b) =
let dict = new Dictionary<'a, 'b>()
let memoizedFunc (input : 'a) =
match dict.TryGetValue(input) with
| true, x -> x
| false, _ ->
// Evaluate and add to lookup table
let answer = f input
dict.Add(input, answer)
@nickleeh
nickleeh / fibonacciRecursiveMemoization.fs
Created May 13, 2015 12:37
Memoization of recursive functions
// Example 7-20. Memoization of recursive functions
> // WRONG way to memoize the function - fib doesn't call the
// memoized version recursively.
let wrongMemFib =
let rec fib x =
match x with
| 0 | 1 -> 1
| 2 -> 2
| n -> fib (n - 1) + fib (n - 2)
memoize fib;;
let toBigInt (n: int) = bigint(n)
let fac n =
[1..n]
|> List.map toBigInt
|> List.reduce (*)
type Duck () =
member this.sayQuack = printfn "quack, quack..."
type Bird () =
member this.sayQuack = printfn "tweet, tweet..."
type Dog () =
member this.bark = printfn "woof, woof..."
let duck = Duck()
library(matlab)
as.logical(isprime(7))
as.logical(isprime(42))
randNorm <- rnorm(3000)
randDensity <- dnorm(randNorm)
require(ggplot2)
ggplot(data.frame(x = randNorm, y = randDensity)) + aes(x = x, y = y) + geom_point() + labs(x = "Random Normal Variables", y = "Density")
@nickleeh
nickleeh / javascript_resources.md
Last active August 29, 2015 14:13 — forked from jookyboi/javascript_resources.md
Here are a set of libraries, plugins and guides which may be useful to your Javascript coding.

Libraries

  • jQuery - The de-facto library for the modern age. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers.
  • Backbone - Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.
  • AngularJS - Conventions based MVC framework for HTML5 apps.
  • Underscore - Underscore is a utility-belt library for JavaScript that provides a lot of the functional programming support that you would expect in Prototype.js (or Ruby), but without extending any of the built-in JavaScript objects.
  • lawnchair - Key/value store adapter for indexdb, localStorage
@nickleeh
nickleeh / 0_reuse_code.js
Last active August 29, 2015 14:13
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console