Skip to content

Instantly share code, notes, and snippets.

Avatar

Richard Gomes frgomes

View GitHub Profile
@frgomes
frgomes / slick_schema_more_than_22_fields.scala
Last active Aug 10, 2019
Scala/Slick :: More than 22 fields using nested case classes
View slick_schema_more_than_22_fields.scala
//----------------------------------------------------------------------------------------------------------------------
// This is an example of how more than 22 fields could be mapped with Slick 3.3.1 (other versions may work as well?).
//
// The general idea is pretty simple:
// 1. define a case class made of nested case classes.
// 2. define a projection made of nested projections.
//
// In addition, you can just get rid of noisy usages of ``GetResult`` generated by the Slick Code Generator.
// Just get rid of that, since usage of ``mapTo[T]`` makes definition of projections clean and hygienic.
//----------------------------------------------------------------------------------------------------------------------
@frgomes
frgomes / openssl_pfx.sh
Last active Jul 1, 2019
openssl - Convert from .PFX to CRT, CER, root PEM and client PEM
View openssl_pfx.sh
#!/bin/bash
# This lists the contents of a PFX file
# This is the last resort in case everything else fails.
# Just see its contents as a text file, cut whatever you need from it and
# paste manually onto separate files.
openssl pkcs12 -info -in input.pfx
# Found this in the internet. Not tested at all!
openssl pkcs12 -in input.pfx -out mycerts.crt -nokeys -clcerts
@frgomes
frgomes / thunderbird_exchange_server.md
Last active May 28, 2019
Thunderbird - connect to Exchange Server
View thunderbird_exchange_server.md
@frgomes
frgomes / instanceOf.rs
Created Apr 11, 2019
Rust - instanceOf
View instanceOf.rs
enum FooBar {
Foo(u8),
Bar(u8)
}
fn test(v: bool) -> FooBar {
if v {
FooBar::Foo(5)
} else {
FooBar::Bar(10)
@frgomes
frgomes / git_rebase1.sh
Created Apr 4, 2019
git - rebase (B from A from master) onto (B from master)
View git_rebase1.sh
#!/bin/bash
git rebase --onto master A B
@frgomes
frgomes / ChainingTry.scala
Last active Nov 18, 2020
Scala - Chaining Try/Success/Failure, return first successful
View ChainingTry.scala
def a: Try[String] = Failure(new RuntimeException)
def b: Try[String] = Failure(new RuntimeException)
def c: Try[String] = Success("C")
def d: Try[String] = Failure(new RuntimeException)
def e: Try[String] = Success("E")
def f: Try[String] = Failure(new RuntimeException)
val result = a orElse b orElse c orElse d orElse e orElse f
// result is Success("C")
@frgomes
frgomes / RegexExtractorDomainName.scala
Last active Jun 22, 2021
Scala - Pattern match extractors, Regex domain names, IPv4, IPv6
View RegexExtractorDomainName.scala
/** Parse any IPv4 address or IPv6 address or domain name */
def parsePeerAddress(value: String): Try[String] =
parseIPv4(value) orElse parseIPv6(value) orElse parseHostname(value)
/** Parse IPv4 address */
def parseIPv4(value: String): Try[String] =
value.trim match {
case regexIPv4(_*) => Success(value)
case _ => Failure(new IllegalArgumentException(s"invalid IPv4 address name: ${value.trim}"))
}
View TypelevelPolymorphic.scala
object TypelevelPolymorphic extends App {
object square extends Poly {
import scala.language.reflectiveCalls
implicit val is = at[Int] { i => i * i }
implicit val fs = at[Float] { f => f * f }
implicit val ds = at[Double] { d => d * d }
}
trait Poly {
View EscapeNonASCII.scala
def escapeNonASCII(str: String) : String = {
val result = new java.lang.StringBuilder
var i = 0
while(i < str.length) {
val cp: Int = Character.codePointAt(str, i)
val n : Int = Character.charCount(cp)
if(n > 1) {
i += n-1
if(i >= str.length) throw new IllegalArgumentException("truncated unexpectedly")
}
View DesignByContract.scala
def addNaturals(nats: List[Int]): Int = {
require(nats forall (_ >= 0), "List contains negative numbers")
nats.foldLeft(0)(_ + _)
} ensuring(_ >= 0)