Skip to content

Instantly share code, notes, and snippets.

View gclaramunt's full-sized avatar

Gabriel Claramunt gclaramunt

View GitHub Profile
@gclaramunt
gclaramunt / topten.scala
Last active December 19, 2015 15:48
top ten words for a text s
def topN(s:String,n:Int)=s.split(' ').groupBy(identity).map({case (w,ls)=>w->ls.length }).toSeq.sortBy(_._2).reverse.take(n)
@gclaramunt
gclaramunt / Mono.scala
Created July 17, 2013 14:45
Monotonicity count
import scala.annotation.tailrec
/**
* User: gabriel
* Date: 7/17/13
*/
object Mono {
def findMonotonicityBreaks(items:Seq[Int]):Int={
@tailrec
def breaksRec(itms:Seq[Int],currMono:Option[Int], count:Int):Int=itms match {
case x::y::ys=> {
object Multifold{
// monotyped "multifold"
def multiFold[A,B](fs:List[(A,B)=>A])(zeros:List[A])(xs:List[B]):List[A]=
xs.foldLeft(zeros)(
(as,b)=> fs.zip(as).map({case (f,a)=>f(a,b)}) // or better: applicative functor
)
}
https://github.com/fedesilva/tm2013-hello-play-scala.git
https://github.com/fedesilva/tm2013-hello-akka.git
https://github.com/fedesilva/tm2013-reactive-stocks.git
@gclaramunt
gclaramunt / Regular1.scala
Created January 21, 2014 13:44
The code is probably incorrect, but it doesn't report the line where the error happens.
import scala.language.higherKinds
object Regular1 {
trait Bifunctor[F[_, _]] {
def bimap[A, R, B, S](fa: F[A, R], f: A => B, g: R => S): F[B, S]
}
trait Regular2[T[_]]{
def from2[A,PF2[_,_]]:T[A]=>PF2[A,T[A]]
@gclaramunt
gclaramunt / Trick.scala
Last active August 29, 2015 13:56
Evil trick using implicits to customize error messages in a validation
abstract class MyTry[T] {
def get: T
def flatMap[U](f: T => MyTry[U]): MyTry[U]
def map[U](f: T => U): MyTry[U]
def filter(p: T => Boolean)(implicit msg:Msg[T]): MyTry[T]
}
object MyTry {
def apply[T](r: => T): MyTry[T] =
@gclaramunt
gclaramunt / all.scm
Created April 22, 2014 01:35
Lot of exercises from "The Little Schemer"
#lang scheme
(define atom? (let ((f1 pair?) (f2 not)) (lambda (x) (f2 (f1 x)))))
(define lat?
(lambda (lat)
(cond
((null? lat) #t)
((atom? (car lat)) (lat? (cdr lat)))
(else #f)
)))
@gclaramunt
gclaramunt / Factory.scala
Created June 10, 2014 16:29
Attempt to a typesafe factory patter in scala
/**
* Created by claramun on 6/9/14.
*/
trait A
trait B { val value:String }
trait X1A extends A
trait X1B extends B
@gclaramunt
gclaramunt / unboxed.hs
Created July 11, 2014 16:38
Dangeours haskell
{-# LANGUAGE MagicHash #-}
-- | Main entry point to the application.
module Main where
import GHC.Exts
-- | The main entry point.
main :: IO ()
main = do
putStrLn $ showUnboxedInt 1#
@gclaramunt
gclaramunt / middle.hs
Created July 11, 2014 21:59
Middle of a list with the hare and tortoise method
-- | Main entry point to the application.
module Main where
-- | The main entry point.
main :: IO ()
main = do
putStrLn "Welcome to FP Haskell Center!"
putStrLn "Have a good day!"
putStrLn $ show $ middle [1]
putStrLn $ show $ middle [1,2]