Skip to content

Instantly share code, notes, and snippets.

@chris-martin
chris-martin / argonaut-lens.scala
Created August 26, 2015 09:26
Examples of using a lens to modify JSON with Argonaut http://argonaut.io/doc/zipper/
import argonaut._, Argonaut._
val obj: Json = Parse.parseOption("""{"a":{"b":"x","c":2}}""").get
val a = jObjectPL >=> jsonObjectPL("a") >=> jObjectPL
(a >=> jsonObjectPL("b") >=> jStringPL).mod(_ + "y", obj)
// res0: argonaut.Json = {"a":{"b":"xy","c":2}}
(a >=> jsonObjectPL("c") >=> jNumberPL)

LensFamily:

A Lens Family, offering a purely functional means to access and retrieve a field transitioning from type B1 to type B2 in a record simultaneously transitioning from type A1 to type A2. scalaz.Lens is a convenient alias for when A1 =:= A2, and B1 =:= B2.

The term "field" should not be interpreted restrictively to mean a member of a class. For example, a lens family can address membership of a Set.

PLensFamily:

Partial Lens Families, offering a purely functional means to access and retrieve an optional field transitioning from type B1 to type B2 in a record that is simultaneously transitioning from type A1 to type A2. scalaz.PLens is a convenient alias for when A1 =:= A2, and B1 =:= B2.

<div ng-app="app">
<div ng-controller="Ctrl1">
<select ng-model="foo" ng-options="o as o.name for o in options">
</select>
</div>
<div ng-controller="Ctrl2">
<select ng-model="foo" ng-options="o.id as o.name for o in options">
</select>
scala> val a = List(Vector(1), List(1))
a: List[scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int]{def companion: scala.coll
ection.generic.GenericCompanion[scala.collection.immutable.Seq with scala.collection.AbstractSeq{def dropRig
ht(n: Int): scala.collection.immutable.Seq[Any] with scala.collection.AbstractSeq[Any]; def takeRight(n: Int
): scala.collection.immutable.Seq[Any] with scala.collection.AbstractSeq[Any]; def drop(n: Int): scala.colle
ction.immutable.Seq[Any] with scala.collection.AbstractSeq[Any]; def take(n: Int): scala.collection.immutabl
e.Seq[Any] with scala.collection.AbstractSeq[Any]; def slice(from: Int,until: Int): scala.collection.immutab
le.Seq[Any] with scala.collection.AbstractSeq[Any]}]; def dropRight(n: Int): scala.collection.immutable.Seq[
Int] with scala.collection.AbstractSeq[In...
val a = ListBuffer(1, 2, 3)
val b = a.toIterable.asInstanceOf[List[Int]]
// List(1, 2, 3)
a.append(4, 5, 6)
b
// List(1, 2, 3, 4, 5, 6)
package foo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
public class OptionalTest {
static class Foo<T> {
from mixpanel import Mixpanel
import json
class MixpanelProject(object):
def __init__(self, token):
self.mixpanel = Mixpanel(token)
def cookie_name(self):
class LazyInit(object):
def __init__(self, cls, *args, **kwargs):
self.wrapped = None
self.cls = cls
self.args = args
self.kwargs = kwargs
def __getattr__(self, key):
if self.wrapped is None:
from django.core.cache import cache
from contextlib import contextmanager
from datetime import timedelta
class LockUnavailable(Exception):
pass
class Lock(object):
from django import template
register = template.Library()
@register.filter
def joinby(value, arg):
return arg.join(map(unicode, value))