Skip to content

Instantly share code, notes, and snippets.

case class Event(name: String, start: Int, end: Int)
def validateName(name: String): Option[String] =
if (name.size > 0) Some(name) else None
def validateEnd(end: Int): Option[Int] =
if (end < 3000) Some(end) else None
def validateStart(start: Int, end: Int): Option[Int] =
if (start <= end) Some(start) else None
val raw: List<Char> = "aabcccccaaa".map { it }
println(raw.toList().fold(listOf<Char>()) { acc, value ->
if (acc.isEmpty()) listOf(value)
else {//a2, b
if (acc.last() == value) acc
else acc + value
}
})
@mcalavera81
mcalavera81 / MessingAround.java
Created October 30, 2015 15:44
Infinite stream of prime numbers
package com.example;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class MessingAround {
helm ls #list releases
helm test #run tests
#installs a release from a remote repo or local folder, in <namespace> and with extra configuration values values.yaml
#helm install <release> <chart_name|local folder> -n <namespace> -f <file_location>
helm install elasticsearch elastic/elasticsearch -n elasticsearch-poc -f ./values.yaml
helm install nginx nginx # the first nginx is the name of the release, the second one is the name of the folder
helm delete <release> #delete a release.
helm delete elasticsearch
#renders charts templates locally
helm template <release> <chart> [flags]
POST /tmdb/_close
POST /tmdb/_open
PUT /tmdb/_settings
{
"analysis": {
"filter": {
"my_shingle": {
"type": "shingle",
"min_shingle_size": 2,
ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); #B MapType mapType =
mapper.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class);
MapType yamlConfigType = mapper .getTypeFactory() .constructMapType( HashMap.class, mapper.getTypeFactory().constructType(String.class), mapType);
Path configFilePath;
Map<String, Map<String, Object>> config = mapper.readValue(configFilePath.toFile(), yamlConfigType);
AuthStrategy authStrategy = null;
Map<String, Object> authConfig = config.get("auth")
if (authConfig.get("strategy").equals(USERNAME_PASSWORD_STRATEGY)) {
authStrategy = new UsernamePasswordAuthStrategy( (String) authConfig.get("username"), (String) authConfig.get("password"));
@mcalavera81
mcalavera81 / request-deduplication
Created August 12, 2021 09:39
request deduplication
function UpdateChatRoom(req: UpdateChatRoomRequest): ChatRoom {
if (req.requestId === undefined) { // #A
return ChatRoom.update(...);
}
const hash = crypto.createHash('sha256').update(JSON.stringify(req)).digest('hex');
const cachedResponse = cache.get(req.requestId);
if (!cachedResult) { // #B
const response = ChatRoom.update(...);
cache.set(req.requestId, { response, hash }); // #C
return response;
@mcalavera81
mcalavera81 / retries-exp-back-off
Last active August 11, 2021 22:54
exponential back-off algorithm
async function getChatRoomWithRetries(id: string, maxDelayMs = 32, maxRetries = 10): Promise<ChatRoom> {
return new Promise<ChatRoom>(async (resolve, reject) => {
let retryCount = 0;
let delayMs = 1000;
while (true) {
try {
return resolve(GetChatRoom({ id }));
} catch (e) {
if (retryCount++ > maxRetries) return reject(e);
await new Promise((resolve) => {
@mcalavera81
mcalavera81 / CompletableFutureTest.java
Created October 22, 2015 18:14
CompletableFuture with Timeouts
package test;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Random;
import java.util.concurrent.*;
import java.util.function.Function;