Skip to content

Instantly share code, notes, and snippets.

View igstan's full-sized avatar

Ionuț G. Stan igstan

View GitHub Profile
/**
* DERIVING THE Y COMBINATOR IN 7 EASY STEPS
*
* Ionut G. Stan | ionut.g.stan@gmail.com | http://igstan.ro | http://twitter.com/igstan
*
*
* The Y combinator is a method of implementing recursion in a programming
* language that does not support it natively (actually, it's used more for
* exercising programming brains). The requirement is the language to support
* anonymous functions.
@igstan
igstan / jitted.c
Created November 30, 2022 20:08
Hello World JIT for Apple Silicon
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/mman.h>
static uint8_t code[] = {
0x40, 0x05, 0x80, 0x52, // mov w0, #3
0xc0, 0x03, 0x5f, 0xd6, // ret
module Seq = struct
include Seq
let rec fold_right : 'a Seq.t -> 'b -> ('a -> 'b Lazy.t -> 'b Lazy.t) -> 'b Lazy.t =
fun seq acc f ->
match seq () with
| Seq.Nil -> lazy acc
| Seq.Cons (a, rest) -> f a (lazy (Lazy.force (fold_right rest acc f)))
let map : ('a -> 'b) -> 'a Seq.t -> 'b Seq.t =
@igstan
igstan / MoneyTest.java
Created July 25, 2011 22:26
Force operations on same money currencies in Java at compile time.
package ro.igstan.playground;
import java.util.ArrayList;
import java.util.List;
public class MoneyTest {
public static interface Money<E extends Money<E>> {
public static final class Zero<E extends Money<E>> implements Money<E> {
@Override
trait SACVisitor[R] {
def c1(a: Int): R
def c2(a: R, b: R): R
}
sealed trait SAC extends Product with Serializable {
def fold[R](visitor: SACVisitor[R]): R
}
object SAC {
<?php
/**
* Copyright (c) 2009, Ionut Gabriel Stan. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
@igstan
igstan / RecordingApplicative.hs
Last active August 22, 2021 10:17
An Applicative that records the instructions of underlying computations.
{-# LANGUAGE DeriveFunctor #-}
import Control.Applicative
import Control.Monad.Writer
import Control.Monad.Reader
import Control.Monad.Identity
import Data.Map (Map, (!))
import qualified Data.Map as Map
import Data.List (groupBy, intercalate, nub)
import Data.Function (on)
import cats.implicits._
import cats.Monad
import cats.data.State
//
// Two capability/tagless final traits: Foo & Bar
//
trait Foo[F[_]] {
def foo: F[Unit]
}
$ ./to-csv.sed records.xml
111111H2,111AA2026
111111N1,111AA2026
111111Q1,111AA2026
111111U1,111AA2026
111111Z1,111AA2026
import cats.Eq
import monix.reactive.Observable
import monix.execution.Scheduler.Implicits.global
object Main {
implicit final class ObservableOps[A](private val self: Observable[A]) extends AnyVal {
def bubbleUpHot(a: A)(implicit Eq: Eq[A]): Observable[A] =
self.publishSelector { hot =>
val special = hot.find(_ === a)
val beforeSpecial = hot.takeWhile(_ =!= a)