Skip to content

Instantly share code, notes, and snippets.

@ms-tg
ms-tg / memoize1.java
Created January 24, 2012 17:40
Scala 'lazy val' vs Java memoization
/**
* Without worrying about concurrency, we can simply cache the return value,
* using null to indicate that it has not yet been calculated...
*/
public class Memoize1 extends Original {
private Integer total;
@Override
public int total() {
if (total == null) {
@estliberitas
estliberitas / check-markdown-links.js
Last active September 8, 2017 10:11
Script checking that all links with references are present in Markdown file.
@jeffreyolchovy
jeffreyolchovy / RecursiveStreams.scala
Created August 30, 2012 15:15
Recursive Streams in Scala
import scala.math.{BigInt, BigDecimal}
object RecursiveStreams
{
// natural numbers
lazy val N: Stream[BigInt] = Stream.cons(BigInt(1), N.map(_ + 1))
// fibonacci series
lazy val fib: Stream[BigInt] = Stream.cons(BigInt(0), Stream.cons(BigInt(1), fib.zip(fib.tail).map(a => a._1 + a._2)))
@developmentalmadness
developmentalmadness / HttpClient.scala
Created February 23, 2016 22:27
Make an http GET request with Akka Http
package com.dvMENTALmadness
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE GADTs #-}
{-# OPTIONS_GHC -Wall #-}
module Main where
@carymrobbins
carymrobbins / NumericLits.hs
Last active November 16, 2018 23:42
Huge hack to support explicit numeric literals in Haskell
{- TODO: The Num and Fractional instances don't implement all methods.
- In practice this is fine but for completeness they should be done.
-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
module NumericLits where
data I = I
i :: I
@dcastro
dcastro / usage.scala
Last active December 6, 2018 12:39
"Newtype" deriving for scala
import org.scalacheck.cats.implicits._
import org.scalacheck.Gen
import io.circe.{Decoder, Encoder}
object Test extends App {
case class X(i: Int)
implicit val x: Encoder[X] = Wrapper[X].deriveInstance[Encoder]
implicit val y: Decoder[X] = Wrapper[X].deriveInstance[Decoder].withErrorMessage("some custom error msg")
val z: Gen[X] = Wrapper[X].lift(Gen.choose(0, 10))
@ChrisPenner
ChrisPenner / GenericMonoid.hs
Last active February 22, 2019 19:51
Derive Monoid for using Generics
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DeriveGeneric #-}
module GenericMonoid where
import Data.Monoid
import Data.Maybe
@oxbowlakes
oxbowlakes / 3nightclubs.scala
Created May 13, 2011 15:14
A Tale of 3 Nightclubs
/**
* Part Zero : 10:15 Saturday Night
*
* (In which we will see how to let the type system help you handle failure)...
*
* First let's define a domain. (All the following requires scala 2.9.x and scalaz 6.0)
*/
import scalaz._
import Scalaz._
@kritzcreek
kritzcreek / Kinds-and-do-syntax.md
Last active March 28, 2020 11:51
Kinds and Do-Syntax

Kinds

In PureScript there are types of different kinds. Kinds are types for types. For example Int has kind Type, and we write it as Int :: Type. You can ask for the kind of a type in purs psci

> :k Int
Type

Type constructors take types to other types. For example Array (which still needs another type to form a type a value could have, like Array Int):