Skip to content

Instantly share code, notes, and snippets.

View igstan's full-sized avatar

Ionuț G. Stan igstan

View GitHub Profile
@igstan
igstan / RecordingApplicative.hs
Last active August 22, 2021 10:17
An Applicative that records the instructions of underlying computations.
{-# LANGUAGE DeriveFunctor #-}
import Control.Applicative
import Control.Monad.Writer
import Control.Monad.Reader
import Control.Monad.Identity
import Data.Map (Map, (!))
import qualified Data.Map as Map
import Data.List (groupBy, intercalate, nub)
import Data.Function (on)
package com.example.http4splayground.services
import java.util.Date
import java.util.concurrent.TimeUnit
import cats._
import cats.data.StateT
import cats.effect.{ExitCode, IO, IOApp}
import cats.implicits._
import cats.mtl.MonadState
@igstan
igstan / kind-polymorphism.scala
Last active May 16, 2018 13:59 — forked from szeiger/gist:3a2e7e65b641bac9764a9c02de233ab9
Kind-polymorphic Any and Nothing
scala> import scala.language.higherKinds
import scala.language.higherKinds
scala> def f[C[_], E]: List[C[E]] = Nil
f: [C[_], E]=> List[C[E]]
scala> f[List, Int]
res0: List[List[Int]] = List()
scala> f[Any, Int]
// libraryDependencies += "com.rklaehn" %% "intervalset" % "0.2.0"
import java.time.Instant
import scala.collection.immutable.SortedSet
import com.rklaehn.interval.IntervalMap.{ Value, FromBool => IntervalMap }
import org.scalatest.FunSuite
import spire.algebra.Order
import spire.implicits.StringOrder
import spire.math.Interval
import spire.optional.genericEq.generic
@igstan
igstan / hogof.py
Last active April 2, 2018 16:02
Higher-Order Gang of Four
# hogof — Higher-Order Gang of Four
# data Fix s a = In{out :: s a (Fix s a)}
def flatten(xss):
return [
x
for xs in xss
for x in xs
]
import scala.concurrent.Future
import cats.~>
import cats.data.ReaderT
import cats.free.Free
object FreeMonads {
sealed trait Op[A]
object Op {
final case class Get[T](name: String) extends Op[T]

Typeclasses In Scala

Here's a common object-oriented problem in OO languages such as Java. You're using two libraries. One of them provides a class, say Foo, which performs some useful tasks. The other library provides a useful bar method that takes as an argument instances of Barable, an interface. You've got no control on either of the two libraries, but you'd like to be able to pass an instance of Foo to the bar method. Let's image it would save you a lot of work to do this. However you can't. Foo doesn't implement Barable and you can't modify Foo to have it implement the aforementioned interface. The normal solution

# Factorial in Eve
## Base Case
```eve
search
r = [#factorial n: 0]
bind
r.return := 1
```
## Recursive Case
indirect enum Type: CustomStringConvertible {
case Int
case Fun(Type, Type)
var isFun: Bool {
switch self {
case .Fun: return true
case _: return false
}
}