Skip to content

Instantly share code, notes, and snippets.

@akimboyko
akimboyko / IronPythonScript.cs
Last active Apr 19, 2016
Simple performance and readability test: RoslynScript vs. IronPython from C# Performance ratio 1:2,7-3 Readability: both scripting engines required some knowledge about underlying APIs
View IronPythonScript.cs
async void Main()
{
GetType().Assembly.FullName.Dump();
IEnumerable<Model.ProcessingModel> models =
Enumerable.Range(0, 1000000)
.Select(n => new Model.ProcessingModel { InputA = n, InputB = n * 0.5M, Factor = 0.050M });
var sw = Stopwatch.StartNew();
@akimboyko
akimboyko / quotes.txt
Last active Jan 5, 2016 — forked from mausch/gist:8227399
Quotes by Erik Meijer from Reactive programming course
View quotes.txt
"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"
View gist:8584781
public static class EnumerableEx
{
public static IEnumerable<R> Select<T1, T2, R>(this IEnumerable<Tuple<T1, T2>> source, Func<T1, T2, R> f)
{
return source.Select(t => f(t.Item1, t.Item2));
}
}
Enumerable.Range(1, 10)
.Select(x => Tuple.Create(x, x))
@akimboyko
akimboyko / DI_patterns.fs
Created Jan 12, 2014
DI patterns from Scala adopted to F#: structural typing and currying
View DI_patterns.fs
// Inspired by Scala and http://skov-boisen.dk/?p=289
type Cell() =
override m.ToString() = "*"
type IGameOfLife =
abstract member Next: IGameOfLife
abstract member Generation : seq<Cell>
// Types for Structural Typing
@akimboyko
akimboyko / BinaryTreeCheck.scala
Created Dec 22, 2013
Properties proved by ScalaCheck about BinaryTree from Reactive course week #005
View BinaryTreeCheck.scala
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 Dec 15, 2013
Game of Life implemented on Scala during Global Day of Coderetreat 2013 at Kiev
View GameOfLife.scala
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 Dec 5, 2013
Why is Future.always useful? Why would I use it if its value is already precomputed?
View 1readme.md

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.

View producer_consumer.scala
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 Dec 29, 2015
How to acheive tail call optimization while traversing tree-like structure using continuation-passing style http://stackoverflow.com/q/20164061/443366
View traverseTree.scala
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 Nov 8, 2013
The Sieve of Eratosthenes — is a simple, ancient algorithm for finding all prime numbers up to any given limit Implemented using Scala and F#
View Sieve.fsx
// 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