Skip to content

Instantly share code, notes, and snippets.

View orionll's full-sized avatar

jkozlov orionll

View GitHub Profile
@orionll
orionll / climb.scala
Last active December 25, 2015 02:48
import java.text.DecimalFormat
import java.util.Locale
import scala.collection.mutable.ListBuffer
import scala.xml.XML
import org.joda.time.DateTime
object Main extends App {
Locale.setDefault(Locale.US)
object World extends App {
val pnt = Point(10, 10)
val str: String = pnt(toString)
val x: Int = pnt(getX)
val pnt2: Point = pnt(setX)(20)
val pnt3: Point = pnt(setXY)(20, 19)
println(str)
println(x)
@orionll
orionll / tabs.sml
Last active December 26, 2015 09:49
How ML code looks like with tabs
fun all_except_option(str, lst) =
case lst of
[] => NONE
| x :: xs =>
if same_string(str, x)
then SOME xs
else case all_except_option(str, xs) of
NONE => NONE
| SOME xs' => SOME (x :: xs')
@orionll
orionll / gist:9708659
Created March 22, 2014 15:07
Effective fold() implementation for IndexedSeq
def myFold[A](seq: IndexedSeq[A])(z: A)(f: (A, A) => A): A = {
// Divide the sequence into to halfes and then combine the results
def recur(from: Int, to: Int): A = {
if (from > to) {
z
} else if (from == to) {
seq(from)
} else {
f(recur(from, from + (to - from) / 2), recur(from + (to - from) / 2 + 1, to))
}
@orionll
orionll / Main.java
Created March 24, 2014 17:02
Pattern matching in Java via exceptions
public class Main {
public static void main(String[] args) {
System.out.println(example());
}
public static String example() {
try {
throw match();
} catch (Int x) {
@orionll
orionll / Main.java
Last active August 29, 2015 13:57
Example which demonstrates how Scala scales out while Java does not
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import com.google.common.base.Stopwatch;
import com.google.common.util.concurrent.Uninterruptibles;
// Тайпкласс Animal (для полноты картины добавил метод hello, иначе был бы совсем тривиальный пример)
// Animal полностью абстрагирован от низлежашего типа A
trait Animal[A] {
def word: String
def talk() { println(word) }
def hello(a: A): String
}
// Обертка над тайпклассом Animal, чтобы можно было писать cat.hello, а не animal.hello(cat)
// AnimalOps полностью дублирует методы, определённые в Animal, но также может содержать еще и дополнительные методы.
@orionll
orionll / Typeclasses.scala
Last active June 9, 2020 12:46
Typeclass example
// Тайпкласс Animal (для полноты картины добавил метод hello, иначе был бы совсем тривиальный пример)
// Animal полностью абстрагирован от низлежашего типа A
trait Animal[A] {
def word: String
def talk() { println(word) }
def hello(a: A): String
}
// Обертка над тайпклассом Animal, чтобы можно было писать cat.hello, а не animal.hello(cat)
// AnimalOps полностью дублирует методы, определённые в Animal, но также может содержать еще и дополнительные методы.
import scalaz.NonEmptyList
import scalaz.syntax.monad._
import scalaz.syntax.equal._
import scalaz.syntax.foldable._
import scalaz.std.option._
import scalaz.std.list._
import scalaz.std.list.listSyntax._
import scalaz.std.anyVal._
import scalaz.std.string._
import scalaz.effect.IO
@orionll
orionll / FizzBuzz.scala
Created December 9, 2014 14:57
FizzBuzz implementation with Scalaz
import scalaz.Applicative
import scalaz.syntax.enum._
import scalaz.syntax.show._
import scalaz.std.anyVal._
import scalaz.effect.IO
import scalaz.effect.IO._
object Main extends App {
def fizzBuzz(i: Int): String = if (i % 3 === 0 && i % 5 === 0) "FizzBuzz"