Skip to content

Instantly share code, notes, and snippets.

View taig's full-sized avatar

Niklas Klein taig

View GitHub Profile
@cranst0n
cranst0n / PostGISCodec.scala
Created August 18, 2021 15:17
Skunk PostGIS codecs.
// Copyright (c) 2018-2021 by Rob Norris
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT
package skunk
package codec
import cats.Eq
import cats.syntax.all._
import skunk.data.Type
@kripod
kripod / Box.tsx
Last active March 27, 2024 18:14
Superseded by https://www.kripod.dev/blog/behind-the-as-prop-polymorphism-done-well/ – Polymorphic `as` prop for React components with TypeScript
import React from 'react';
// Source: https://github.com/emotion-js/emotion/blob/master/packages/styled-base/types/helper.d.ts
type PropsOf<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
E extends keyof JSX.IntrinsicElements | React.JSXElementConstructor<any>
> = JSX.LibraryManagedAttributes<E, React.ComponentPropsWithRef<E>>;
export interface BoxOwnProps<E extends React.ElementType = React.ElementType> {
as?: E;
@Daenyth
Daenyth / CachedResource-Blog.md
Last active March 26, 2024 17:19
CachedResource for cats-effect

Concurrent resource caching for cats

Motivation

cats-effect Resource is extremely handy for managing the lifecycle of stateful resources, for example database or queue connections. It gives a main interface of:

trait Resource[F[_], A] {
  /** - Acquire resource
    * - Run f
 * - guarantee that if acquire ran, release will run, even if `use` is cancelled or `f` fails
@calvinlfer
calvinlfer / README.md
Last active April 24, 2024 13:03
Developing an understanding of the FS2 Pull datatype

Developing an understanding of the FS2 Pull data type

In Pull[F, O, R], R is the return type. Pull represents a computation that emits some values on the Stream (of type O) and returns a new thing (R). In order to convert a Pull to a Stream, R must be Unit. This is because an FS2 Stream does not have the ability to terminate with a return value.

image

See here for the conversation

  • Stream[F, O] is monadic over O which are the output values emitted
  • Pull[F, O, R] is monadic over R which is used for stateful transformations
@sha1n
sha1n / GuidV5.java
Last active October 21, 2022 03:31
Type 5 UUID generator in Java & Scala
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
public class GuidV5 {
public static UUID nameUUIDFrom(String name) throws UnsupportedEncodingException, NoSuchAlgorithmException {
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");

Thread Pools

Thread pools on the JVM should usually be divided into the following three categories:

  1. CPU-bound
  2. Blocking IO
  3. Non-blocking IO polling

Each of these categories has a different optimal configuration and usage pattern.

@hrules6872
hrules6872 / CustomLayout.java
Created February 23, 2017 23:01
Custom Viewgroup: onSaveInstanceState - onRestoreInstanceState http://trickyandroid.com/saving-android-view-state-correctly/
public class CustomLayout extends LinearLayout {
@SuppressWarnings("unchecked") @Override public Parcelable onSaveInstanceState() {
Parcelable saveInstanceState = super.onSaveInstanceState();
SavedState savedState = new SavedState(saveInstanceState);
savedState.childrenStates = new SparseArray();
for (int i = 0; i < getChildCount(); i++) {
getChildAt(i).saveHierarchyState(savedState.childrenStates);
}
return savedState;
import java.io.{File, FileInputStream, FileOutputStream}
import monix.eval.Task
import monix.execution.Ack
import monix.execution.Ack.{Continue, Stop}
import monix.reactive.{Consumer, Observable, Observer}
import scala.util.control.NonFatal
def copyFile(input: File, destination: File, chunkSize: Int): Task[Unit] =
Task.defer {
val in = new FileInputStream(input)
package dao
import scalaz.concurrent.Task
import scalaz.stream.Process
import doobie.imports._
import scalaz._
import Scalaz._
import models.Persisted

Applied Functional Programming with Scala - Notes

Copyright © 2016-2018 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x