Skip to content

Instantly share code, notes, and snippets.

Jason Webb bigjason

Block or report user

Report or block bigjason

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@bigjason
bigjason / partial.py
Created Apr 23, 2019
Partial function application supporting any arg order (WIP)
View partial.py
_sentinal = object()
@dataclass(frozen=True)
class Placeholder(DataClassEnrichment):
keyword: Optional[str] = None
default: Optional[Any] = _sentinal
annotation: Type = Any
def type_repr_of(self, obj):
"""Return the repr() of an object, special-casing types (internal helper).
View foo.scala
implicit class EitherTCompanionExt(wrapper: scalaz.EitherT.type) {
class PinnedEitherT[F[_]] {
def apply[A,B](a: Future[A \/ B]): scalaz.EitherT[Future, A, B] = scalaz.EitherT[Future, A, B](a)
}
@inline def eitherTPinned[F[_]] = new PinnedEitherT[F]
@inline def eitherTFuture[A, B](a: Future[A \/ B]): scalaz.EitherT[Future, A, B] = eitherTPinned[Future](a)
}
View foo.scala
package object {
implicit def nelFormat[A](implicit format: Format[A]): Format[scalaz.NonEmptyList[A]] = new Format[scalaz.NonEmptyList[A]] {
import scalaz._, Scalaz._
override def writes(o: NonEmptyList[A]) = JsArray(o.toList.map(Json.toJson(_)))
override def reads(json: JsValue) = Json.fromJson[List[A]](json) match {
case JsSuccess(Nil, _) => JsError("empty list not valid")
case JsSuccess(head :: rest, _) => JsSuccess(NonEmptyList.nel(head, rest))
case JsError(errors) => JsError(errors)
@bigjason
bigjason / RicherTry.scala
Last active Mar 31, 2016
How the hell is `scala.util.Try` missing a `zip` function?
View RicherTry.scala
/*
This is free and unencumbered software released into the public domain.
*/
implicit class RicherTry[+T](wrapped: Try[T]) {
def zip[That](that: => Try[That]): Try[(T, That)] =
for (a <- wrapped; b <- that) yield (a, b)
}
View factorial.scala
@scala.annotation.tailrec
def fact(n: Int, acc: BigInt = 1): BigInt =
if(n < 0) throw new Exception("n must be >= 0")
else if (n == 0) acc
else fact(n - 1, acc * n)
View foo.scala
scala> Array(1).hashCode
res16: Int = 357917741
scala> Array(1).hashCode
res17: Int = 1064821544
scala> Seq(1).hashCode
res18: Int = 1945410391
scala> Seq(1).hashCode
View blues.json
[
"Red",
"Blue",
"Gray or Grey",
"DarkBlue",
"Black",
"Orange",
"Purple",
"Brown",
"Maroon",
View drunk.scala
scala> val mayBeLong: Option[Long] = Option(null: java.lang.Long)
java.lang.NullPointerException
at scala.Predef$.Long2long(Predef.scala:358)
... 33 elided
scala> val mayBeLong: Option[Long] = Option(null: java.lang.Long).flatMap(Option.apply _)
<console>:7: error: type mismatch;
found : java.lang.Long => Option[java.lang.Long]
required: java.lang.Long => Option[scala.Long]
val mayBeLong: Option[Long] = Option(null: java.lang.Long).flatMap(Option.apply _)
@bigjason
bigjason / DayOneMapOps.scala
Last active Aug 29, 2015
Extension to add missing `Map` transformers.
View DayOneMapOps.scala
package mapops {
implicit class DayOneMapOps[A, +B](wrapped: Map[A, B]) {
// Scalaz has `mapKeys` to just change keys
// Scala has `mapValues` to just change values
// Scala has `transform` to just change values but provides key as a parameter
/**
* This function transforms all the keys and values of mappings contained in this map with function `f`.
You can’t perform that action at this time.