Skip to content

Instantly share code, notes, and snippets.

View filosganga's full-sized avatar

Filippo De Luca filosganga

View GitHub Profile
@pathikrit
pathikrit / SudokuSolver.scala
Last active April 12, 2024 15:00
Sudoku Solver in Scala
val n = 9
val s = Math.sqrt(n).toInt
type Board = IndexedSeq[IndexedSeq[Int]]
def solve(board: Board, cell: Int = 0): Option[Board] = (cell%n, cell/n) match {
case (r, `n`) => Some(board)
case (r, c) if board(r)(c) > 0 => solve(board, cell + 1)
case (r, c) =>
def guess(x: Int) = solve(board.updated(r, board(r).updated(c, x)), cell + 1)
val used = board.indices.flatMap(i => Seq(board(r)(i), board(i)(c), board(s*(r/s) + i/s)(s*(c/s) + i%s)))
object Test {
import scalaz.{Lens,State}
case class BlahConfig(server: Int, l: List[Int])
def doStuff(i: Int) = State[BlahConfig,Int]( c => (c.copy(l = i :: c.l), c.server) )
case class FooConfig(val password: String)
def doSomeStuff(i: Int) = State[FooConfig, String](c =>(c, c.password))
@obolton
obolton / elb-nodejs-ws.md
Last active November 12, 2023 11:49
Configuring an AWS Elastic Load Balancer for a Node.js application using WebSockets on EC2

AWS ELB with Node.js and WebSockets

This assumes that:

  • You are using Nginx.
  • You want to accept incoming connections on port 80.
  • Your Node.js app is listening on port 3000.
  • You want to be able to connect to your Node.js instance directly as well as via the load balancer.

####1. Create load balancer

@johnynek
johnynek / Future.rs
Created November 13, 2014 04:20
Future with map and monadic bind in Rust.
use std::comm::{Receiver, channel};
use std::io;
use std::mem::replace;
use std::task::spawn;
struct Future<'a, A> {
state: FutureState<'a, A>
}
@jdegoes
jdegoes / lambdaconf-2014.md
Last active August 29, 2015 13:58
The LambdaConf 2014 Challenge

Introduction

LambdaConf has multiple tracks, so no attendee can possibly attend all sessions.

Yet, we'd like to schedule the sessions to maximize attendee happiness (for some definition of "maximum" and "happiness").

Sounds like a job for functional programming!

Your mission, should you choose to accept it, is to write a small functional program to produce an "optimal" schedule (your schedule may have any number of tracks you want -- you do not have to stick with 3 or 4).

@t32k
t32k / foobar.md
Created September 25, 2013 02:05
foo, bar, baz, qux, quux, corge, grault, garply, waldo, fred, plugh, xyzzy, thud
@johnynek
johnynek / twophase.scala
Last active October 2, 2020 22:36
A two-phase commit Transaction Monad. See: http://en.wikipedia.org/wiki/Two-phase_commit_protocol
// Run this with scala <filename>
/**
* A Two-phase commit Monad
*/
trait Transaction[+T] {
def map[U](fn: T => U): Transaction[U] = flatMap { t => Constant(fn(t)) }
def flatMap[U](fn: T => Transaction[U]): Transaction[U] =
FlatMapped(this, fn)
@vmadman
vmadman / logstash-supervisord
Created April 27, 2013 06:48
An example SupervisorD configuration for all three logstash components. Some of it might look obvious, but it took a ton of tweaking to figure it out. (but I might just be dumb)
[program:lss]
process_name=Shipper
command=java -jar /usr/local/logstash/bin/logstash-1.1.9-monolithic.jar agent --config /usr/local/logstash/conf/shipper.conf --log /usr/local/logstash/log/shipper.log
user=logstash
startretries=3
redirect_stderr=true
std_out_logfile=NONE
startsecs=3
environment=HOME="/usr/local/logstash/"
@viktorklang
viktorklang / ExecutionContextExecutorServiceBridge.scala
Last active June 27, 2022 11:38
Turning an ExecutionContext to an ExecutorService (or rather and ExecutorService AND an ExecutionContext) using Scala 2.10+
/*
Copyright 2013 Viktor Klang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
@vlfig
vlfig / TestCaseClassInterning.scala
Created September 20, 2012 17:27
Traits to easily make classes intern their instances assuring no duplicates exist at runtime. Don't forget to check your equals and hashCode.
import scala.collection.mutable.WeakHashMap
/**
* Utility traits for classes whose companion objects are to
* intern their instances, never creating repeated objects.
*
* Instances are cached based on their immutable
* arguments, as provided to companion object's {{apply()}}.
*/