Skip to content

Instantly share code, notes, and snippets.

View AlexRogalskiy's full-sized avatar
🛰️
Work on stuff that matters

Alexander AlexRogalskiy

🛰️
Work on stuff that matters
View GitHub Profile
@AlexRogalskiy
AlexRogalskiy / js_object_creator.md
Last active September 13, 2020 23:18
JS object creation strategies

Different ways to create objects in JavaScript

JavaScript has a number of predefined objects. In addition, you can create your own objects.

It is sometimes referred to as creating objects with literal syntax. It's one of easiest way to create an object by simply define the property and values inside curly braces.

@AlexRogalskiy
AlexRogalskiy / cartesian.ps1
Created December 6, 2020 10:56
Cartesian product (Powershell)
function CartesianProduct {
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Hashtable]
$values = @{ Foo = 1..5; Bar = 1..10}
)
$keys = @($values.GetEnumerator() | ForEach-Object { $_.Name })
$result = @($values[$keys[0]] | ForEach-Object { @{ $keys[0] = $_ } })
if ($keys.Length -gt 1) {
@AlexRogalskiy
AlexRogalskiy / symlinks.ps1
Last active December 17, 2020 21:53
Create symlinks (windows)
@AlexRogalskiy
AlexRogalskiy / Factory_method.scala
Last active December 19, 2020 19:56
Scala Pattern: factory method
trait Animal
private class Dog extends Animal
private class Cat extends Animal
trait Wanna
case object WannaCat extends Wanna
case object WannaDog extends Wanna
trait AnimalFactory[Wanna] {
def create(): Animal
@AlexRogalskiy
AlexRogalskiy / java_extension.sql
Created December 19, 2020 20:12
Java extension for Postgres
SET pljava.libjvm_location TO '/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/amd64/server/libjvm.so';
CREATE EXTENSION pljava;
GRANT USAGE ON LANGUAGE java TO postgres;
ALTER DATABASE postgres SET pljava.libjvm_location FROM CURRENT;
SELECT sqlj.install_jar( 'file:///tmp/simple-java-function/target/simple-java-function.jar','jfunctions', true );
SELECT sqlj.set_classpath( 'public', 'jfunctions' );
@AlexRogalskiy
AlexRogalskiy / reader_monad.scala
Created December 19, 2020 20:15
Scala reader monad
/**
* A monad to abstract dependencies in the code, see https://coderwall.com/p/kh_z5g
*/
object Reader {
/**
* an implicit to convert a function A => B in a Reader[A, B]
*/
implicit def reader[C, R](block: C => R): Reader[C, R] = Reader(block)
@AlexRogalskiy
AlexRogalskiy / reader_mapping_monad.scala
Created December 19, 2020 20:20
Scala reader monad mapping
import scala.collection._
import scala.collection.generic._
import scala.concurrent.{ Future, ExecutionContext }
/**
* a Typeclass representing a class that can map and flatMap (collections, Option, Future..).
* effectively, it's a Monad without enforcing the axioms of a Monad.
*/
trait CanMap[A, B, M[_]] {
def map(l: M[A])(f: A => B): M[B]
@AlexRogalskiy
AlexRogalskiy / range_monoid.scala
Last active December 19, 2020 20:26
Scala monoid range
object MonoidRange {
case class MonoidRangeBuilder[T](from: T, to: T, incl: Boolean)(implicit mo: Monoid[T], ordering: Ordering[T]) {
def by(step: T) = Stream.iterate(from) { previous: T =>
previous + step
}.takeWhile { x =>
if (incl) {
ordering.lteq(x, to)
} else {
ordering.lt(x, to)
@AlexRogalskiy
AlexRogalskiy / refreshing_cache.scala
Created December 19, 2020 20:28
Scala refreshing cache
import com.twitter.util.{Duration, Time}
/**
* Sort of a lazy val, but that refreshes itselfs every so often; or a temporary cache for a value T,
* will be refreshed at a minimun at the specified interval.
* In practice, the refresh is done only when the value is accessed, so there are no guarantees
* to when it will actually be refreshed. You can just be sure that it won't be refreshed if two calls
* are made within `every`.
*/
class RefreshEvery[T](every: Duration)(refresh: => T) {
@AlexRogalskiy
AlexRogalskiy / retry.scala
Last active December 19, 2020 20:29
Scala retry
import scala.concurrent.Await
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.concurrent.blocking
import scala.concurrent.duration.Deadline
import scala.concurrent.duration.Duration
import scala.concurrent.duration.DurationInt
import scala.concurrent.duration.DurationLong
import scala.concurrent.future
import scala.concurrent.promise