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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Factorial in Eve | |
## Base Case | |
```eve | |
search | |
r = [#factorial n: 0] | |
bind | |
r.return := 1 | |
``` | |
## Recursive Case |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
indirect enum Type: CustomStringConvertible { | |
case Int | |
case Fun(Type, Type) | |
var isFun: Bool { | |
switch self { | |
case .Fun: return true | |
case _: return false | |
} | |
} |