Skip to content

Instantly share code, notes, and snippets.

View malzzz's full-sized avatar

Mallory M. malzzz

  • Bivalent Capital
  • /dev/null
View GitHub Profile
@viktorklang
viktorklang / Future-retry.scala
Last active July 23, 2023 23:48
Asynchronous retry for Future in Scala
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import akka.pattern.after
import akka.actor.Scheduler
/**
* Given an operation that produces a T, returns a Future containing the result of T, unless an exception is thrown,
* in which case the operation will be retried after _delay_ time, if there are more possible retries, which is configured through
* the _retries_ parameter. If the operation does not succeed and there is no retries left, the resulting Future will contain the last failure.
@xuwei-k
xuwei-k / FreeT.scala
Last active September 12, 2016 00:26
import scalaz._
object Tram {
type Tram[A] = FreeT[Function0, Id.Id, A]
implicit val instance: Monad[Tram] = FreeT.freeTMonad[Function0, Id.Id]
def suspend[A](a: => Tram[A]): Tram[A] =
instance.point(a).flatMap(conforms)
@shinnya
shinnya / ReaderMonad.scala
Last active September 10, 2017 06:22
Reader Monad
/*
* Copyright (C) 2017 Shinya Yamaoka
* Licensed under the MIT License.; you may not use this file except in
* compliance with the license. You may obtain a copy of the License at
* https://opensource.org/licenses/mit-license.php
*/
object ReaderMonad {
class Reader[-E, +R] private (g: E => R) {
def apply(env: E): R = g(env)
@nairbv
nairbv / gist:7923811
Created December 12, 2013 06:02
An example of using a Manifest to get around type erasure
scala> def foo[T : Manifest](a : List[T]) = {
| val m = implicitly[Manifest[T]]
| val I = classOf[Int]
| val S = classOf[String]
| m.erasure match {
| case I => "hi"
| case S => "bye"
| case _ => "whatever"
| }
| }
@judotens
judotens / bitcoinkey.sh
Last active June 26, 2024 20:49
Scrape BitCoin private keys from directory.io
#!/bin/bash
# scrape all leaked bitcoin private keys into a tab separated text
# <private key>\t<bitcoin_address>
#
# support autoresume. just add these line into your cron : * * * * bash bitcoinkey.sh
# results stored on keys.txt
if [ ! -f last.page ]; then prev=`echo 0`; else prev=`cat last.page`; fi;
if [ -z $1 ]; then akhir=`echo 10`; else akhir=`echo $1`; fi;
abis=$(($prev+$akhir))
@loosechainsaw
loosechainsaw / fuckyeahreadermonad.scala
Created November 6, 2013 12:16
Fuck Yeah Reader Monad
package examples.monads
case class User(id: Int, username: String, name: String)
trait UserRepository{
def getById(id: Int) : Option[User]
def save(user: Option[User]) : Unit
}
trait UserService{
@larsrh
larsrh / church.scala
Created September 24, 2013 19:55
Church encoding in Scala
// rank 1
type num[a] = (a => a) => a => a
def zero[a]: num[a] = f => x => x
def succ[a](n: num[a]): num[a] = f => x => f(n(f)(x))
def eval(n: num[Int]): Int = n(_ + 1)(0)
// rank 2
@viktorklang
viktorklang / CallbackExecutionContext.scala
Created September 7, 2013 21:42
Creates an ExecutionContext inside an Actor that will execute the runnables inside the Actor receive block, so that the risk of closing over Actor internals is less problematic.
//Warning: Uses Akka internal APIs (not binary compatible),
//can be written not to depend on that but implemented like this for brevity.
package akka.util
trait CallbackExecutionContext { this: akka.actor.Actor ⇒
// Defines our signal for callback execution
case object Execute
// ``SerializedSuspendableExecutionContext`` is Akka-internal
private[this] final val ssec: SerializedSuspendableExecutionContext = {
@mergeconflict
mergeconflict / Inject.scala
Created May 28, 2013 18:16
"dependency injection" monad (for the lulz)
case class Inject[-E, +A](inject: E => A) {
// covariant functor and monad
def map[B](f: A => B): Inject[E, B] =
Inject { e => f(inject(e)) }
def flatMap[ε <: E, B](f: A => Inject[ε, B]): Inject[ε, B] =
Inject { e => f(inject(e)).inject(e) }
// to satisfy for-comprehension desugaring in scala < 2.10
def filter(f: A => Boolean): Inject[E, A] =
@Mortimerp9
Mortimerp9 / readerwithtooling.scala
Created April 14, 2013 22:14
An implementation of the Reader Monad in scala, with correct type variance and some implicit utils to simplify the daily use of Readers, In particular with Future.
/**
* A monad to abstract dependencies in the code, see https://coderwall.com/p/kh_z5g
*/
object Reader {
/**
* an implicit to convert a function A => B in a Reader[A, B]
*/
implicit def reader[C, R](block: C => R): Reader[C, R] = Reader(block)