Skip to content

Instantly share code, notes, and snippets.

View milanboers's full-sized avatar
🙃

Milan Boers milanboers

🙃
View GitHub Profile
@milanboers
milanboers / mapf.go
Created July 21, 2022 13:41
Map function over arguments golang
func mapF[T any, R any](f func(T) (R, error), values ...T) ([]R, error) {
result := make([]R, len(values))
for _, value := range values {
mappedValue, err := f(value)
if err != nil {
return nil, fmt.Errorf("mapF failure: %w", err)
}
result = append(result, mappedValue)
}
return result, nil
@milanboers
milanboers / deep_merge.py
Created November 12, 2020 12:56
Deep merge dicts in Python
def deep_merge(dict1: dict, dict2: dict) -> dict:
""" Merges two dicts. If keys are conflicting, dict2 is preferred. """
def _val(v1, v2):
if isinstance(v1, dict) and isinstance(v2, dict):
return deep_merge(v1, v2)
return v2 or v1
return {k: _val(dict1.get(k), dict2.get(k)) for k in dict1.keys() | dict2.keys()}
@milanboers
milanboers / read_s3_python.py
Last active October 13, 2020 08:11
Streaming read lines from S3 in Python using boto
s3 = boto3.client('s3')
obj = s3.get_object(Bucket=bucket, Key=key)
lines = map(lambda x: x.decode('utf-8'), obj['Body'].iter_lines()) # Iterator which yields lines in file
@milanboers
milanboers / delete-merged-branches.sh
Last active July 12, 2021 08:34
Delete all merged git branches on remote origin. Excludes "master" and "develop" branches.
git fetch -p; git branch -r --merged master | grep -Ev 'master|develop|main' | sed 's/origin\///' | xargs -n1 git push --delete origin
@milanboers
milanboers / multi4.sh
Last active September 28, 2017 12:10
Execute 4 commands in split screen in tmux
#!/bin/bash
# Usage: ./multi4.sh first_command second_command third_command fourth_command
tmux send-keys "$1" 'C-m'
tmux split-window -v
tmux send-keys "$2" 'C-m'
tmux split-window -h
tmux send-keys "$3" 'C-m'
tmux select-pane -U
tmux split-window -h
tmux send-keys "$4" 'C-m'
@milanboers
milanboers / CacheConfiguration.java
Created August 14, 2017 10:57
Spring Cache + Ehcache 3 configuration (JCache / JSR107)
@Configuration
@EnableCaching
public class CacheConfiguration {
@Bean
public JCacheManagerFactoryBean cacheManagerFactoryBean() throws Exception {
JCacheManagerFactoryBean jCacheManagerFactoryBean = new JCacheManagerFactoryBean();
jCacheManagerFactoryBean.setCacheManagerUri(new ClassPathResource("ehcache.xml").getURI());
return jCacheManagerFactoryBean;
}
@milanboers
milanboers / MyComponent.java
Created July 31, 2017 13:18
Enable/disable Spring component by property
@Component
@Conditional(MyComponent.EnabledCondition.class)
public class MyComponent {
protected static class EnabledCondition implements Condition {
private static final String ENABLED_PROPERTY = "mycomponent.enabled";
@Override
public boolean matches(final ConditionContext context, final AnnotatedTypeMetadata metadata) {
final Environment environment = context.getEnvironment();
return environment != null && Boolean.valueOf(environment.getProperty(ENABLED_PROPERTY));
}
@milanboers
milanboers / listOfFuturesToFutureList.java
Created May 17, 2017 10:04
Convert a List of CompletableFutures to a CompletableFuture of a List
public static <T> CompletableFuture<List<T>> listOfFuturesToFutureList(final List<CompletableFuture<T>> listOfFutures) {
final Function<List<CompletableFuture<T>>, List<T>> transformFunction = futures -> futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList());
return CompletableFuture.allOf(listOfFutures.toArray(new CompletableFuture[0]))
.thenApply(v -> transformFunction.apply(listOfFutures));
}
@milanboers
milanboers / clone.bash
Last active September 12, 2024 11:52
Clone all repositories of a Github user
curl -s https://api.github.com/users/milanboers/repos | grep \"clone_url\" | awk '{print $2}' | sed -e 's/"//g' -e 's/,//g' | xargs -n1 git clone
@milanboers
milanboers / MillerRabin.scala
Last active May 1, 2016 05:12
Deterministic version of the Miller-Rabin primality test for Scala
//MillerRabin.scala
import scala.language.implicitConversions
object MillerRabin {
implicit class MillerRabin(n: Long) {
private lazy val asl : Array[BigInt] = if (n < 2047L) Array(2L)
else if (n < 1373653L) Array(2L, 3L)
else if (n < 9080191L) Array(31L, 73L)
else if (n < 25326001L) Array(2L, 3L, 5L)
else if (n < 3215031751L) Array(2L, 3L, 5L, 7L)