Skip to content

Instantly share code, notes, and snippets.

@danslapman
danslapman / LList.cs
Last active December 9, 2015 09:11
Immutable Linked List
using System.Collections;
using System.Collections.Generic;
namespace Immutable
{
public sealed class LList<T> : IReadOnlyCollection<T>
{
public T Head { get; }
public LList<T> Tail { get; }
public bool IsEmpty { get; }
@danslapman
danslapman / InlineOps.fs
Last active December 11, 2023 23:22
InlineOps
let inline is_null (x:^T when ^T : not struct) = obj.ReferenceEquals (x, null)
let inline (!>) (x:^a) : ^b = ((^a or ^b) : (static member op_Implicit : ^a -> ^b) x)
let inline (^) f x = f x
let inline (~%) x = ignore x
@danslapman
danslapman / state.scala
Created October 16, 2016 15:07
StateT as a monad
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scalaz.{Monad, StateT}
import scalaz.std.AllInstances._
object Test extends App {
implicit val ifsmi = StateT.stateTMonadState[Int, Future]
import ifsmi.{get, put}
@danslapman
danslapman / memoize.py
Created December 6, 2016 12:37
Python memoization decorator
__author__ = 'Daniil <danslapman> Smirnov'
__copyright__ = 'Copyleft 2013, danslapman'
__contact__ = 'https://bitbucket.org/danslapman/memoize'
def memoize(cache):
def memoize(fun):
def memoizator(*args, **kwargs):
if args in cache:
return cache[args]
else:
@danslapman
danslapman / aux.scala
Created December 7, 2016 19:08
Selectors
val book =
('author ->> "Benjamin Pierce") ::
('title ->> "Types and Programming Languages") ::
('id ->> 262162091) ::
('price ->> 44.11d) ::
HNil
def readBook[B <: HList](book:B)(implicit
author: Selector.Aux[B, Witness.`'author`.T, String],
title: Selector.Aux[B, Witness.`'title`.T, String],
@danslapman
danslapman / main.yml
Created March 1, 2017 14:11
Login to ecr
---
- name: Obtain Amazon ECR token
shell: |
docker run -i --rm \
-e "AWS_KEY={{ amazon_ecr_aws_key }}" \
-e "AWS_SECRET_KEY={{ amazon_ecr_aws_secret_key }}" \
opedge/awscli:latest \
aws ecr --region us-east-1 get-authorization-token
register: ecr_token_result
@danslapman
danslapman / NumericPow.scala
Last active February 21, 2018 14:30
Generic power operator
implicit class PowerOp[T <: AnyVal](val value: T) extends AnyVal {
import Numeric.Implicits._
import scala.{math => scmath}
@inline def **(power: T)(implicit numeric: Numeric[T]): Double =
scmath.pow(value.toDouble(), power.toDouble())
}
@danslapman
danslapman / exc.py
Created April 7, 2017 13:34
Print exception with line number
import linecache
def PrintException():
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
print 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj)
@danslapman
danslapman / LazyFuture.scala
Created October 25, 2017 18:49
Lazy future
type LazyFuture[T] = ReaderT[Future, ExecutionContext, T]
object LazyFuture {
def apply[T](value: => T): LazyFuture[T] = ReaderT.apply { implicit ExecutionContext =>
Future(value)
}
}
@danslapman
danslapman / example.scala
Created November 10, 2017 09:02
Custom atom
package ru.tinkoff.testApi
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import io.circe.generic.JsonCodec
import ru.tinkoff.tschema.akkaHttp.{MkRoute, Serve}
import ru.tinkoff.tschema.swagger.SwaggerTypeable
import ru.tinkoff.tschema.typeDSL.DSLAtom
import shapeless.{HList, Witness}