Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env zsh
# in fino veritas
# Borrowing shamelessly from these oh-my-zsh themes:
# fino-time
# pure
# https://gist.github.com/smileart/3750104
# Set required options
@lunaspeed
lunaspeed / functional.scala
Created June 19, 2020 01:54
scala functional
def sum(li: List[Int]): Int = {
var total = 0
for (i <- li) {
total = total + i
}
total
//只能加Int?
@lunaspeed
lunaspeed / functional.go
Created June 19, 2020 00:48
go to functional
func sum(list []int) int {
var t int
for _, i := range list {
t += i
}
return t
}
//只能加總數字?
@lunaspeed
lunaspeed / sample.go
Last active June 19, 2020 10:25
multi-process sample
func DoAndGetInt() (int, error) {
//do something for long time
return 1, nil
}
func DoAndGetString() (string, error) {
//do something for long time
return "1", nil
}
@lunaspeed
lunaspeed / csrf.go
Created May 29, 2020 02:03
Ruby CSRF token generation and verification in Golang
package csrf
import(
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/theckman/go-securerandom"
)
const AuthenticityTokenLength = 32
@lunaspeed
lunaspeed / java_date_localdate.scala
Last active June 22, 2018 07:59
Java: Convert Date to LocalDate/LocalDateTime and back (Scala syntax)
//reference http://www.baeldung.com/java-date-to-localdate-and-localdatetime
//LocalDate to Date
val date: Date = Date.from(LocalDate.now.atStartOfDay(ZoneId.systemDefault).toInstant)
//Date to LocalDate
val input = new Date
val ldate = input.toInstant.atZone(ZoneId.systemDefault).toLocalDate
//Java 9
val ldate9 = LocalDate.ofInstant(input.toInstant, ZoneId.systemDefault)
@lunaspeed
lunaspeed / Transfer.java
Created April 26, 2018 04:23
Transfer Problem
import java.math.BigDecimal;
public final class Transfer {
private Transfer(){}
public static void transfer(User from, User to, BigDecimal amount) {
synchronized (from) {
if(from.getBalance().compareTo(amount) >= 0) {
synchronized (to) {
import java.math.BigDecimal;
public final class Transfer {
private Transfer(){}
public static void transfer(User from, User to, BigDecimal amount) {
synchronized (from) {
if(from.getBalance().compareTo(amount) >= 0) {
synchronized (to) {
@lunaspeed
lunaspeed / HttpCreator.scala
Last active May 29, 2020 02:05
Ignore SSL certificate in Apache HttpClient 4.5.x using Scala
object HttpCreator {
def createHttpClientAcceptsUntrustedCerts(cookieStore: CookieStore): CloseableHttpClient = {
val httpClient: CloseableHttpClient = {
val trustStrategy = new TrustStrategy {
override def isTrusted(x509Certificates: Array[X509Certificate], s: String): Boolean = true
}
val sslContext = new SSLContextBuilder().loadTrustMaterial(null, trustStrategy).build()
val sslSocketFactory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier)
@lunaspeed
lunaspeed / gist:e87b3e24ac9e6a231cc0757003f87b14
Last active January 8, 2018 03:45
快快樂樂裝docker
sudo curl -sSL https://get.docker.com/ | sh
sudo usermod -aG docker $(whoami)
sudo curl -L "https://github.com/docker/compose/releases/download/1.16.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo apt-get install -y bash-completion