Skip to content

Instantly share code, notes, and snippets.

@alkagin
alkagin / sumTree
Created November 3, 2014 19:11
Example of tail recursion in scala
sealed abstract class Tree
case class Leaf(x: Int) extends Tree
case class Node(a: Tree, b: Tree) extends Tree
def sumTree(t: Tree): Int = {
def sumTreeHelper(trees: List[Tree], acc: Int): Int = trees match {
case Nil => acc
case Leaf(x) :: result => sumTreeHelper(result, x + acc)
@alkagin
alkagin / gist:f75133e598421019b97c
Last active August 29, 2015 14:13
Exercise 2.2 of Functional Programming In Scala
FILE "isSorted.scala"
def isSorted[A](as: Array[A], ordered: (A,A) => Boolean): Boolean = {
@annotation.tailrec
def go(n: Int): Boolean =
if (n >= as.length-1) true
else if (!ordered(as(n), as(n+1))) false
else go(n+1)
go(0)
}
-----BEGIN PGP MESSAGE-----
Version: GnuPG v1
owGtkltsTEEYx3dbWhUEaURKXSZSHtaaOdc5jTTBm3qouERTsWbmzNk9us5ZeymL
jQcRWkpdSmQbLSpuJaJEpGzckj40sZJKNYgXl4QqIiRFXM5pKqmEN/MyM9/8/t/3
n2+mYWyuJ8ebrph+zwNWVHi7XlPPimcvBrYAautJULoFsLDJrbi7ssh6DkpBNU9S
EuN+055h2Tr3r4vNGGJ8oIZHY6ZtORT0Yz9SQMrn8q6ah3UeiweqTd09RRBRIqgU
MiIQAapElgxJgLpgaAbGnMuKjA2mYZkwBToTFjk2ZMxULHAIdapQBolT0DCtII9G
oqZrEUiciYQhJmoSM0QIuSxxpxLRqKrrmkCxpHLEKXSEITsW/+MyYNBpYNDeX/j/
7DsxmI5SjAwo6bpqiIqmiFgVRMy4yLCsCwhpLhjj0aHGk3A1CZqW21MnWGMyPuxV
@alkagin
alkagin / CassandraResultSetSink.java
Created June 22, 2016 07:22
CassandraResultSetSink
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
import java.util.concurrent.atomic.AtomicInteger
import cats._
import cats.effect._
import cats.effect.implicits._
object foo extends IOApp {
import fs2._
val ai = new AtomicInteger()
override def run(args: List[String]): IO[ExitCode] =