Skip to content

Instantly share code, notes, and snippets.

View gabro's full-sized avatar

Gabriele Petronella gabro

View GitHub Profile
@gabro
gabro / modalValueProducer.swift
Last active August 29, 2015 14:27
UIViewController producing values. Example of editing a profile section with a modal interaction, which can produce a new Patient
// Create a ViewController automatically wrapped in a UINavigationController
// returns the viewController and a Signal<Patient, NoError>
let (profileVC, signal) = EditPersonalInfoViewController.wrappedInNavigationController(patient: patient)
// observe the signal. When a new Patient is produced, update the view
// dismiss the view controller whenever the signal is completed (i.e. modifications have been saved or interaction has been canceled)
signal.observe(next: reloadViewForPatient, completed: dismissViewController)
// let the game being
self.presentViewController(profileVC, animated: true, completion: nil)
@gabro
gabro / boom.scala
Last active September 4, 2015 16:22
Subtle scala quirk with implicit conversions
@ "42".toInt
res0: Int = 42
@ implicit object Foo extends Function1[String, Int] {
def apply(s: String) = s.toInt
}
defined object Foo
@ "42”.toInt
// BOOM (infinite recursion)
@gabro
gabro / FreeConsultations.scala
Last active September 24, 2015 10:35
Learning the Free monad with cats
package project
import cats._
import cats.free.Free
object Main extends App {
import Algebra._
import ConsultationOp._
val c = Consultation(_id = "123", title = "A consultation")
@gabro
gabro / AssetPicker.swift
Created November 2, 2015 10:07
A block-based asset picker for videos, photos and files
import Photos
import MobileCoreServices
class AssetPicker: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIDocumentPickerDelegate, UIDocumentMenuDelegate {
static let sharedInstance = AssetPicker()
private lazy var cameraPicker: UIImagePickerController = {
let cameraPicker = UIImagePickerController()
@gabro
gabro / 0_reuse_code.js
Created July 6, 2016 08:58
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console

Keybase proof

I hereby claim:

  • I am gabro on github.
  • I am gabro (https://keybase.io/gabro) on keybase.
  • I have a public key ASBZl9AJO-rUEOoKZ98kEhpqYfcs3vlfuC1DA_bwE0FdRgo

To claim this, I am signing this object:

@gabro
gabro / validation.flow.js
Last active February 24, 2020 16:38
Dynamic object validation using $ObjMap in Flow
/* @flow */
// A simplified representation of types using phantom types (so that we store the Type information both at value and type level)
class Type<T> {};
class StringT extends Type<string> {}
class NumberT extends Type<number> {}
// A schema for a User
const User = {
name: new StringT(),
@gabro
gabro / applicative.js
Created November 21, 2016 10:22
Applicative abstraction in vanilla.js
const arr = {
ap(fab, fa) {
return fab.reduce((acc, f) => acc.concat(fa.map(f)), [])
}
}
const f = (n) => n * 2
const g = (n) => n + 1
const a1 = [f, g]
@gabro
gabro / compose-applicative.js
Last active November 21, 2016 13:04
Applicative instances composition in vanilla js
function composeFunctor(f, g) {
function map(h, fga) {
return f.map(ga => g.map(h, ga), fga)
}
return { map }
}
function composeApplicative(f, g) {
@gabro
gabro / cats.scala
Created February 25, 2017 10:52
Monad Transformers for the working programmer
import cats.data.OptionT, cats.std.future._
def findAddressByUserId(id: Long): Future[Option[Address]] =
(for {
user <- OptionT(findUserById(id))
address <- OptionT(findAddressByUser(user))
} yield address).value