Skip to content

Instantly share code, notes, and snippets.

Avatar

Hamid hamidr

View GitHub Profile
@graninas
graninas / cpp_stm_free_tutorial.md
Last active Jan 14, 2021
Software Transactional Memory in C++: Pure Functional Approach (tutorial)
View cpp_stm_free_tutorial.md

Software Transactional Memory in C++: pure functional approach (Tutorial)

In this article I’ll tell you about my pure functional library for Software Transactional Memory (STM) that I’ve built in C++. I adopted some advanced functional programming concepts that make it composable and convenient to use. Its implementation is rather small and robust, which differentiates the library from competitors. Let’s discuss what STM is and how to use it.

@annappropriate
annappropriate / UpdateReturning.scala
Created Jun 10, 2016
Update returning for Slick 3.1.1
View UpdateReturning.scala
private def updateReturning[A, F](
returningQuery: Query[A, F, C], v: U): SqlStreamingAction[Vector[F], F, Effect.All] = {
val ResultSetMapping(
_, CompiledStatement(_, sres: SQLBuilder.Result, _), CompiledMapping(_updateConverter, _)) =
updateCompiler.run(updateQuery.toNode).tree
val pconv: SetParameter[U] = {
val ResultSetMapping(_, compiled, CompiledMapping(_converter, _)) =
updateCompiler.run(updateQuery.toNode).tree
val converter = _converter.asInstanceOf[ResultConverter[JdbcResultConverterDomain, U]]
View rt.md

An expression is said to be referentially transparent if it can be replaced with its value without changing the behavior of a program (in other words, yielding a program that has the same effects and output on the same input)

https://en.wikipedia.org/wiki/Referential_transparency

This example is taken from the book "Functional Programming in Scala"

The following is referentially transparent:

scala> val x = "hello, world"
@non
non / operators-for-cats.md
Last active Jan 5, 2018
Basic write-up of how Cats supports using symbolic operators with type classes.
View operators-for-cats.md

Symbolic operators and type classes for Cats

One of the simplest and most recognizable type classes is the semigroup. This type class abstracts over the ability to combine values of a certain type in an associative manner.

Cats provides cats.Semigroup[A] to model semigroups [1]. The combine method takes two values of the type A and returns an A value.

@invkrh
invkrh / mockInput.scala
Last active Nov 24, 2017
input mocking for test need
View mockInput.scala
object test extends App {
import scala.collection.JavaConversions.asJavaEnumeration
import java.io.{ByteArrayInputStream, SequenceInputStream}
val inputs = Iterator("asdf", "qewr", "wert")
.map(x => new ByteArrayInputStream((x + "\n").getBytes))
val in = new SequenceInputStream(asJavaEnumeration(inputs))
Console.setIn(in)
val a = readLine()
val b = readLine()
@azymnis
azymnis / KMeansJob.scala
Created Oct 23, 2014
K-Means in scalding
View KMeansJob.scala
import com.twitter.algebird.{Aggregator, Semigroup}
import com.twitter.scalding._
import scala.util.Random
/**
* This job is a tutorial of sorts for scalding's Execution[T] abstraction.
* It is a simple implementation of Lloyd's algorithm for k-means on 2D data.
*
* http://en.wikipedia.org/wiki/K-means_clustering
@vjache
vjache / SRB
Last active Aug 29, 2015
Deadly simple Shared Ring Buffer in Erlang.
View SRB
%% Shared Ring Buffer
-module(srb).
-export([new/2, push/2, pop/2, pop/1]).
-compile(export_all).
-record(counters,
{ clock :: non_neg_integer(),
max_size :: non_neg_integer() }).
@micmarsh
micmarsh / flip.clj
Last active Feb 6, 2018
Flip the arguments of a function, Clojure style
View flip.clj
(defn flip [function]
(fn
([] (function))
([x] (function x))
([x y] (function y x))
([x y z] (function z y x))
([a b c d] (function d c b a))
([a b c d & rest]
(->> rest
(concat [a b c d])
View Generate Mongo Object ID in PostGres
The MIT License (MIT)
Copyright (c) 2013 Jamar Parris
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE S
@ghoseb
ghoseb / prime_sieve.clj
Last active Apr 22, 2016
A concurrent prime sieve in Clojure using core.async
View prime_sieve.clj
(ns prime-sieve
(:require [clojure.core.async :as async :refer [chan go <! >!]]))
;;; concurrent prime sieve in Clojure using core.async
;; inspired by a similar implementation in Go
;; http://golang.org/doc/play/sieve.go
(defmacro go-forever
"An infinite loop that runs in a go block."
You can’t perform that action at this time.