Skip to content

Instantly share code, notes, and snippets.

View kephas's full-sized avatar

Pierre Thierry kephas

  • AUTOGRIFF
  • Strasbourg, France
  • X @kephasp
View GitHub Profile
@kephas
kephas / readevents.hs
Created April 2, 2024 10:39
Attempt at polymorphic interpreter for Polysemy
#! /usr/bin/env nix-shell
#! nix-shell -i runghc -p "haskellPackages.ghcWithPackages (p: [p.aeson p.polysemy p.polysemy-plugin])"
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
@kephas
kephas / Adhoc.hs
Created October 16, 2023 15:43
Type-safe refactoring with ad-hoc polymorphism
module Adhoc where
data ClientIdentifier
= Admin
| User String
deriving (Eq, Show)
data Settings = Settings
{settingAllowList :: [ClientIdentifier]}
deriving (Show)
@kephas
kephas / Adhoc.hs
Created October 14, 2023 18:19
Type-safe refactoring with ad-hoc polymorphism
module Adhoc where
@kephas
kephas / effectful0.hs
Created August 4, 2023 10:47
Effectful code with different orders of effects in effectful code
#! /usr/bin/env nix-shell
#! nix-shell -i runghc -p "haskellPackages.ghcWithPackages (p: [p.effectful p.effectful-th])"
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GHC2021 #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
@kephas
kephas / conditions.hs
Created March 27, 2023 10:08
Alternative to early return in Functional Programming
checkUserForPromotion :: User -> Either String User
checkUserForPromotion user =
case (user.age > 18, user.country, length user.purchases > 5) of
(True, France, True) -> Right (user { status = Vip })
(False, _, _) -> Left "trop jeune"
(True, _, False) -> Left "pas assez d'achats"
(True, _, True) -> Left "pas français"
<?php
function creer_doublon($max) {
$start = 1;
$nombres = range(1, $max);
array_push($nombres, rand(1, $max));
shuffle($nombres);
return $nombres;
}
print_r(creer_doublon(10));
@kephas
kephas / padded_binary.lisp
Last active March 21, 2019 14:52
Padded binary numbers in Common Lisp
(defun read-only-chars (stream chars)
(with-output-to-string (result)
(loop while (find (peek-char nil stream) chars)
do (princ (read-char stream) result))))
(set-dispatch-macro-character #\# #\! (lambda (stream subchar arg)
(parse-integer (remove #\_ (read-only-chars stream "01_")) :radix 2)))
@kephas
kephas / keybase.md
Created May 27, 2015 21:41
keybase.md

Keybase proof

I hereby claim:

  • I am kephas on github.
  • I am kephasp (https://keybase.io/kephasp) on keybase.
  • I have a public key whose fingerprint is F9B7 AA0C 0DA1 EF01 E495 C5E2 C5ED 7720 D9D5 0D8A

To claim this, I am signing this object:

@kephas
kephas / nlet.cpp
Created March 19, 2015 01:45
Scheme's named let mockup in C++
nlet foo(count = 5) {
if(count != 0) {
cout << count << endl;
foo(count--);
}
@kephas
kephas / nlet.lisp
Created March 19, 2015 01:40
Example of Scheme's named let in action
(defmacro nlet (name bindings &body body)
"This is the LET from RnRS with a name, that creates a local function."
`(labels ((,name ,(mapcar #'first bindings) ,@body))
(,name ,@(mapcar #'second bindings))))
(nlet foo ((count 5))
(unless (= 0 count)
(print count)
(foo (1- count))))