Skip to content

Instantly share code, notes, and snippets.

View akimboyko's full-sized avatar
🙃

Akim Boyko akimboyko

🙃
View GitHub Profile
@akimboyko
akimboyko / quotes.txt
Last active January 5, 2016 13:57 — forked from mausch/gist:8227399
Quotes by Erik Meijer from Reactive programming course
"Hopefully the third answer is right; but who knows, maybe I made a mistake; I’m just a human, I can throw exceptions as well."
"I am waving my hands on purpose here, this is very spaghetti like code. And spaghetti is great as food, but not good as code."
"flatMap will allow us to focus on the happy path. flatMap will take care of all the noise. flatMap is the dolby for programmers."
"Great programmers write baby code"
"it's obviously correct"
@akimboyko
akimboyko / BinaryTreeCheck.scala
Created December 22, 2013 14:08
Properties proved by ScalaCheck about BinaryTree from Reactive course week #005
package actorbintree
import scala.concurrent.duration._
import akka.actor.{Props, ActorSystem}
import akka.testkit._
import org.scalacheck.{Gen, Properties}
import org.scalacheck.Prop._
import actorbintree.BinaryTreeSet._
import actorbintree.BinaryTreeSet.Contains
import actorbintree.BinaryTreeSet.OperationFinished
@akimboyko
akimboyko / GameOfLife.scala
Created December 15, 2013 13:13
Game of Life implemented on Scala during Global Day of Coderetreat 2013 at Kiev
package main.scala.com.coderetreat
object GameOfLife {
// data structure
class Cell(x: Int, y: Int) {
val posX = x
val posY = y
override def toString = "Cell: " + x + "," + y
@akimboyko
akimboyko / 1readme.md
Created December 5, 2013 04:23
Why is Future.always useful? Why would I use it if its value is already precomputed?

Future.always is a way to lift a normal value T to a Future[T] value. This lifting pattern is something you will see often in functional programming, so remember it well!

To make its usefulness more apparent - imagine that your API method should either call some Web service or look in the cached responses to see if the Web service was already queried with that request. In the first case you have to return a future Future[String] of a response, and in the second you need to return a response String right away from your cache. Future.always can help you solve this tricky type situation.

@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.