Skip to content

Instantly share code, notes, and snippets.

View rakeshopensource's full-sized avatar

Rakesh Rathi rakeshopensource

View GitHub Profile
@rakeshopensource
rakeshopensource / Factorial.java
Last active June 25, 2019 09:01
Factorial with Java8 Stream API
import java.math.BigInteger;
import java.util.stream.Stream;
@FunctionalInterface
interface Call<T> {
Call<T> apply();
default boolean isComplete() {
return false;
@rakeshopensource
rakeshopensource / FunctionAndPredicate.java
Created February 17, 2018 16:04
Java8 Function and Predicate Example
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
public class FunctionAndPredicate {
@rakeshopensource
rakeshopensource / LoanPattern.java
Created February 17, 2018 16:18
Loan Pattern using Java8
import java.util.function.Consumer;
class MailBuilder {
private String from;
private String to;
private String subject;
private String body;
public MailBuilder from(final String fromAddress) {
this.from = fromAddress;
@rakeshopensource
rakeshopensource / pc.go
Created February 24, 2018 17:33
Producer Consumer problem implementation in golang
package main
import (
"fmt"
"strconv"
"time"
)
var done = make(chan string)
func producer(c chan<- string) {
@rakeshopensource
rakeshopensource / Prime.java
Created April 5, 2018 08:10
Prime numbers with Java8 Stream API
import java.util.stream.IntStream;
public class Prime {
public static void main(String[] args) {
IntStream.rangeClosed(1, 100)
.filter(i -> IntStream.rangeClosed(2, (int) Math.sqrt(i))
.noneMatch(j -> i % j == 0 && i != 1)
)
.forEach(System.out::println);
@rakeshopensource
rakeshopensource / MapDemo.java
Created April 5, 2018 08:29
Map Sorting using java8 stream api
package com.rakesh;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
@rakeshopensource
rakeshopensource / faninseq.go
Last active January 12, 2019 05:27
Go Fan In and Restoring Sequence
package main
import (
"fmt"
"math/rand"
"time"
)
type Message struct {
msg string
@rakeshopensource
rakeshopensource / googlesearch.go
Last active January 12, 2019 08:52
Fake google search example in Go using concurrent Go routines.
package main
import (
"fmt"
"math/rand"
"time"
)
var (
Web = fakeSearch("web")
@rakeshopensource
rakeshopensource / SingleThreadedHttpServer.java
Created September 14, 2023 15:57
Sample basic single threaded event loop in java implemented using non-blocking IO
package org.rakeshopensource.systemdesign;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
@rakeshopensource
rakeshopensource / BloomFilter.java
Created September 14, 2023 16:02
Simple, basic Bloom filter simulation in Java that uses multiple hash functions to efficiently test if an element might be in a set.
package org.rakeshopensource.systemdesign;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import java.util.function.Function;
public class BloomFilter <T> {
private final int size;
private final BitSet bitSet;