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 / 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 / color.pl
Last active January 2, 2021 20:28
Perl sed emulator
$line1="COLOR_RES(\"%d\",";
$line2="\tscreen.Acolors[%d],";
$line3="\tDFT_COLOR(\"rgb:%2.2x/%2.2x/%2.2x\")),\n";
# colors 16-231 are a 6x6x6 color cube
for ($red = 0; $red < 6; $red++) {
for ($green = 0; $green < 6; $green++) {
for ($blue = 0; $blue < 6; $blue++) {
$code = 16 + ($red * 36) + ($green * 6) + $blue;
printf($line1, $code);
@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 / raspberry-motd.sh
Last active December 27, 2020 12:48
RaspberryPI Motd sample
#!/bin/bash
# This is a fork of https://github.com/gagle/raspberrypi-motd that addresses issue #6.
function color (){
echo "\e[$1m$2\e[0m"
}
function extend (){
local str="$1"
let spaces=60-${#1}

Welcome to Cacher

We're delighted you've chosen Cacher to be your snippet organizer! Whether you're a solo developer or a member of your team, Cacher is here to help you organize and use snippets more efficiently.

Our users create snippets to:

  • Remember project-specific algorithms
  • Create cheatsheets for useful libraries
  • Share knowledge with colleagues
@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 / user_id.scala
Last active December 20, 2020 10:23
User factory
trait ID
case class SequentialID(id: Int) extends ID
case class OtherID(id: Int) extends ID
case class User(val id: ID, val b: Int, val c: Int)
object UserId {
def unapply(w: User) = w match {
case u @ User(SequentialID(id), _, _) => Some(u, id)
@AlexRogalskiy
AlexRogalskiy / config_utils.scala
Last active December 20, 2020 10:23
Scala play framework utils
package utils
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.duration._
import play.api.Application
import collection.JavaConversions._
object ConfigString {
implicit class ConfigStr(s: String) {
def configOrElse(default: FiniteDuration)(implicit app: Application): FiniteDuration =
@AlexRogalskiy
AlexRogalskiy / exception_handling.scala
Last active December 20, 2020 10:23
Scala exception handling
import scala.util.control.Exception._
val numbersAsStrings = Seq("1", "2", "3", "4", "10", "N")
val ints = numbersAsStrings flatMap { catching(classOf[NumberFormatException]) opt _.toInt } filter { _ % 2 == 0 }