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
@sketchytech
sketchytech / gist:5ed3c8241ce1509c5342
Last active May 23, 2020 02:00
Swift: How to draw a clock face using CoreGraphics and CoreText (Part 2: Animating with CABasicAnimation)
// see this blogpost: http://sketchytech.blogspot.co.uk/2014/11/swift-how-to-draw-clock-face-using_12.html
import UIKit
class ViewController: UIViewController {
func rotateLayer(currentLayer:CALayer,dur:CFTimeInterval){
var angle = degree2radian(360)
object TransducerUniversal {
type Reduct[-A, R] = (R, A) => R
trait Trans[+A, -B] { def apply[R](f: Reduct[A, R]): Reduct[B, R] }
def map[A, B](f: A => B): Trans[B, A] = new Trans[B, A] { def apply[R](rf: Reduct[B, R]) = (r, a) => rf(r, f(a)) }
def filter[A](p: A => Boolean): Trans[A, A] = new Trans[A, A] { def apply[R](rf: Reduct[A, R]) = (r, a) => if (p(a)) rf(r, a) else r }
def comp[A,B,C](t1 : Trans[A, B], t2 : Trans[C, A]): Trans[C, B] = new Trans[C, B] { def apply[R](rf: Reduct[C, R]) = t1(t2(rf)) }
def sequence[A, B](t: Trans[B, A], data: Seq[A]) = data.foldLeft(Seq[B]())(t(_ :+ _))
implicit class Compable[A,B](t1: Trans[A, B]) {
@non
non / transducers.scala
Created October 30, 2014 00:39
Slightly simpler example of typed transducers in Scala. See http://blog.podsnap.com/ducers2.html for more context.
object Transducer {
type RF[R, A] = (R, A) => R
def apply[A, B](f: A => B) =
new Transducer[B, A] {
def apply[R](rf: RF[R, B]) = (r, a) => rf(r, f(a))
}
}
import Transducer.RF
package scalax.collection
import scala.collection.mutable.ListBuffer
/** FoldTransformers and the views based on them are a Scala
* adaptation, and to some degree an extension, of Rich Hickey's
* transducers for Clojure. They show that the concepts can be
* implemented in a type-safe way, and that the implementation is
* quite beautiful.
*/
object FoldingViews {
@Klasu
Klasu / DbTransactionMonad.scala
Last active October 14, 2016 07:03
Monad for Database Transactions
// Simple monad to deal with multiple database transactions with same connection, their commits and rollbacks
// Something similar done in http://advorkovyy.blogspot.com.au/2010/10/transactional-monad-for-scala.html
import java.sql.Connection
trait DbTransaction[A] {
def unit: Connection => A
def map[B](f: A => B): DbTransaction[B] = DbTransaction {
connection => f(unit(connection))
}
@ahoy-jon
ahoy-jon / Transducer.scala
Last active April 27, 2017 07:49
Trying to transduce in Scala
package ahoy
import scala.collection.mutable
object transducer {
type Reducer[A, R] = R => A => R
trait Transducer[A, B] {
@runarorama
runarorama / gist:a8fab38e473fafa0921d
Last active April 13, 2021 22:28
Compositional application architecture with reasonably priced monads
sealed trait Interact[A]
case class Ask(prompt: String)
extends Interact[String]
case class Tell(msg: String)
extends Interact[Unit]
trait Monad[M[_]] {
def pure[A](a: A): M[A]
@zackthehuman
zackthehuman / QuadTree.scala
Created June 9, 2014 13:32
Working generic quadtree written in Scala
case class Identifier(id: Long)
case class Rectangle(x: Int, y: Int, width: Int, height: Int)
object QuadTree {
private val MAX_OBJECTS = 2
private val MAX_LEVELS = 5
private val PARENT_NODE = -1
private val TOP_RIGHT = 0
private val TOP_LEFT = 1
@MishaelRosenthal
MishaelRosenthal / DirectedGraphTraversals.scala
Created May 15, 2014 11:02
Directed Graph Traversals Implementations are tail recursive and uses only immutable data structures. These implementations are not more efficient than non tail recursive implementations.
package com.liveperson.predictivedialer.examples.misc
import scala.annotation.tailrec
import scala.util.Try
/**
* Created with IntelliJ IDEA.
* User: mishaelr
* Date: 5/14/14
* Time: 5:18 PM
@kafecho
kafecho / Reader.scala
Created May 8, 2014 08:20
Example of using the Reader Monad.
package org.kafecho.learning.monad
import java.util.UUID
/** My attempt at implementing the Reader Monad concept.
* The Reader Monad encapsulates a computation which:
* - requires a dependency of type D
* - produces values of type A.
*/
case class Reader[D, A](computation: D => A) {