Skip to content

Instantly share code, notes, and snippets.

View akimboyko's full-sized avatar
🙃

Akim Boyko akimboyko

🙃
View GitHub Profile
@akimboyko
akimboyko / producer_consumer.scala
Created December 2, 2013 16:59
Producer/Consumer from section "Futures and Promises" http://docs.scala-lang.org/overviews/core/futures.html
import scala.concurrent.{ future, promise }
import scala.concurrent.ExecutionContext.Implicits.global
val p = promise[T]
val f = p.future
val producer = future {
val r = produceSomething()
p success r
continueDoingSomethingUnrelated()
@akimboyko
akimboyko / traverseTree.scala
Last active December 29, 2015 04:38
How to acheive tail call optimization while traversing tree-like structure using continuation-passing style http://stackoverflow.com/q/20164061/443366
import util.control.TailCalls._
sealed abstract class Tree
case class Leaf(n: Int) extends Tree
case class Node(left: Tree, right: Tree) extends Tree
lazy val numbers = Seq.range(1, 20)
lazy val imbalancedTree = numbers.map(Leaf).foldLeft[Tree](Leaf(0))(Node)
def traverseTree(tree: Tree)(action: Int => Unit): Unit = {
@akimboyko
akimboyko / Sieve.fsx
Created November 8, 2013 07:00
The Sieve of Eratosthenes — is a simple, ancient algorithm for finding all prime numbers up to any given limit Implemented using Scala and F#
// The Sieve of Eratosthenes in Code from FP course on Coursera rewritten on F#
let rec sieve(lazyCell: Lazy<Stream<'a>>) = seq {
match lazyCell.Value with
| LazyCell(a, lazyTail) ->
yield a
yield! sieve(lazyTail) |> Seq.filter (fun n -> n % a > 0)
| _ -> ()
}
// Samples from F# interactive
@akimboyko
akimboyko / Nat.fs
Created October 19, 2013 05:18 — forked from pirrmann/Nat.fs
//An implementation of basic non-negative integers, based on https://gist.github.com/akimboyko/7019648,
//but rewritten in a more F# idiomatic way
module Naturals
open System
type Natural =
| Zero
| Succ of Natural
member x.IsZero' = x = Zero
@akimboyko
akimboyko / Nat.fs
Created October 17, 2013 05:47
Quiz from Functional Programming Principles in Scala by Martin Odersky, lecture 4.2 implemented on F# with unittests
//Provide an implementation of the abstract class Nat that represents non-negative integers
//
//Do not use standard numerical classes in this implementation.
//Rather, implement a sub-object and sub-class:
//
//class Zero : Nat
//class Succ(n: Nat) : Nat
//
//One of the number zero, then other for strictly positive numbers.
namespace Nat
@akimboyko
akimboyko / 1natural_numbers.md
Last active December 25, 2015 17:08
Quiz from Functional Programming Principles in Scala by Martin Odersky, lecture 4.2 rewritten on C#/F#

Provide an implementation of the abstract class Nat that represents non-negative integers

Do not use standard numerical classes in this implementation. Rather, implement a sub-object and sub-class:

class Zero : Nat
class Succ(n: Nat) : Nat

One of the number zero, then other for strictly positive numbers.

@akimboyko
akimboyko / fp_types_and_recirsion_quiz_42.txt
Last active December 25, 2015 09:59
Quiz from Functional Programming Principles in Scala by Martin Odersky, lecture 4.2
Provide an implementation of the abstract class Nat that represents non-negative integers
abstract class Nat {
def isZero: Boolean
def predecessor: Nat
def successor: Nat
def + (that: Nat): Nat
def - (that: Nat): Nat
}
@akimboyko
akimboyko / week#02.scala
Created October 5, 2013 18:46
Functional Programming Principles in Scala week #02 Confusing sample with λ expression
def isCloseEnough(x : Double, y : Double) = {
val tolerance = 0.0001
Math.abs((x - y) / x) < tolerance
}
def fixedPoint(f : Double => Double)(firstGuess : Double) = {
def iterate(guess : Double) : Double = {
val next = f(guess)
if(isCloseEnough(guess, next)) next
else iterate((next + guess) / 2)
@akimboyko
akimboyko / ImmediateFunctions.fs
Created July 30, 2013 05:18
JavaScript-like immediate function in F#, am I losing sanity?
let magicSquare = (fun () ->
let randomSequence =
(seq { 1 .. N * N })
|> Seq.sortBy(fun n -> random.Next())
|> Seq.toArray
Matrix.Generic.init N N
(fun nRow nColumn -> randomSequence.[nRow * N + nColumn]))()
@akimboyko
akimboyko / FivePrincipleOfCodeGeneration.md
Created June 19, 2013 09:52
Five principle of code generation by @KathleenDollard

Five principle of code generation by @KathleenDollard

  1. Code generation has to be under your control, your control being the organization or the individual.
  2. Metadata is discreet and more morphable, and actually metadata is a subgenre all on its own.
  3. Code generation has to be an easy process. It should be a part of the normal continuous integration build process.
  4. Handcrafted code is sacred and protected.
  5. Generated code should be an equal or a better quality than nongenerated code.

From Code Generation and T4 with Kathleen Dollard, Text transcript of show #152