Skip to content

Instantly share code, notes, and snippets.

View L7R7's full-sized avatar

Leonhard Riedißer L7R7

View GitHub Profile
countOccurrences :: (Ord k, Num a) => (t -> k) -> [t] -> Map k a
countOccurrences f xs = Map.fromListWith (+) [(f x, 1) | x <- xs]
groupWith :: (Ord b) => (a -> b) -> [a] -> Map b [a]
groupWith f xs = Map.fromListWith (++) [(f x, [x]) | x <- xs]
@L7R7
L7R7 / partition.hs
Last active January 30, 2020 21:35
ideas for implementing partition and partitionMap from the Scala collection standard lib
partition :: [a] -> (a -> Bool) -> ([a], [a])
partition as f = (filter f as, filter (not . f) as)
partition2 :: [a] -> (a -> Bool) -> ([a], [a])
partition2 as f = foldr combine ([], []) as
where
combine elem (ts, fs) =
if f elem
then (elem : ts, fs)
else (ts, elem : fs)
@L7R7
L7R7 / Covariant.scala
Last active May 10, 2019 14:21
Demonstration of cat's Validated used to validate name, email and age before creating a User object
import cats.data.Validated
class InvalidNameException extends Exception
class InvalidEmailException extends Exception
class InvalidAgeException extends Exception
object Validations {
def validateName(name: String): Validated[InvalidNameException, String] =
if (name.isEmpty) Validated.Invalid(new InvalidNameException())
else Validated.Valid(name)
import io.vavr.collection.Seq;
import io.vavr.control.Validation;
class Demo {
public static void main(String[] args) {
String name = "name";
String email = "ex@mple.com";
int age = 20;
Validation<Seq<Exception>, User> user = Validation.combine(
@L7R7
L7R7 / CovariantEnum.java
Created May 10, 2019 09:42
Demonstration of vavr's Validation used to validate name, email and age before creating a User object
import io.vavr.collection.Seq;
import io.vavr.control.Validation;
class Demo {
public static void main(String[] args) {
String name = "name";
String email = "foo@bar.com";
int age = 20;
Validation<Seq<UserError>, User> user = Validation.combine(
@L7R7
L7R7 / Covariant.java
Created May 10, 2019 09:33
Demonstration of vavr's Validation used to validate name, email and age before creating a User object.
import io.vavr.collection.Seq;
import io.vavr.control.Validation;
class Demo {
public static void main(String[] args) {
String name = "name";
String email = "ex@mple.com";
int age = 20;
Validation<Seq<Exception>, User> user = Validation.combine(
package com.l7r7.lab.feed.client
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers.Link
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.scaladsl.Source
import akka.stream.{ ActorMaterializer, Materializer }
@L7R7
L7R7 / Reader.scala
Created March 23, 2019 17:32
Sample Reader for reading a paginated a HTTP resource
package com.l7r7.lab.feed.client
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.Multipart.General
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers.Link
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{ Flow, Source }