Skip to content

Instantly share code, notes, and snippets.

View salanki's full-sized avatar

Peter Salanki salanki

View GitHub Profile
@salanki
salanki / CiscoNACAppliance.scala
Created February 9, 2011 00:42
Beginning of API implementation for the Cisco NAC Appliance in Scala. Should be easily usable from Java, depends on dispatch-http.
package cisconacpoller
import scala.util.matching.Regex
trait Response
case class FailResponse(reason: String) extends Response
object NoSuchUserFailure extends FailResponse("No logged in user matches the criteria")
object InvalidCredentialsFailure extends FailResponse("Username or password is incorrect")
def union[A](a: Set[A], b: Set[A]) {
var c = b
for(item <- a) {
c += item
}
c
}
OR:
@salanki
salanki / mac2ip.scala
Created July 6, 2011 21:58
Mac2IP implementation
package com.proceranetworks.psm.leasepoller
package twpoller
import java.net.InetAddress
import java.nio.ByteBuffer
object ByteBufferImplicits {
implicit def byteBuffer2PimpedByteBuffer(buffer: ByteBuffer): PimpedByteBuffer = new PimpedByteBuffer(buffer)
}
class PimpedByteBuffer(buffer: ByteBuffer) {
@salanki
salanki / todo.scpt
Created July 15, 2013 12:20
Automatically add TODOs to Inbox of Things from iMessages beginning with "TODO:"
using terms from application "Messages"
on message received theMessage from theBuddy for theChat
set buddyId to id of theBuddy as text
if (makelower(theMessage) begins with "todo:") then
set trim1 to trim_line(theMessage, "todo:", 0)
set trim2 to trim_line(trim1, " ", 0)
tell application "Things 2"
set newToDo to make new to do ¬
with properties {name:trim2, notes:"From: " & buddyId}
@salanki
salanki / gist:8609177
Last active August 27, 2023 16:52 — forked from ceedubs/gist:8589661
import scalaz._
import Scalaz._
import scala.concurrent.Future
import scalaz.contrib.std.scalaFuture._ // typeclass instances for Scala Future
import scala.concurrent.ExecutionContext.Implicits.global // implicit execution context...you probably want a better one for real-world
type ErrorsOrT[M[+_], +A] = EitherT[M, NonEmptyList[String], A] // String could be your error type of choice
for {
thing1 <- EitherT(Future.successful(1.right))
@salanki
salanki / coproductEncoder.scala
Last active August 26, 2022 00:39
Coproduct Encoder for Argonaut
import shapeless.{Poly1, Coproduct}
import shapeless.ops.coproduct.Folder
import argonaut.{_}
import argonaut.Argonaut._
/**
* Generic encoder for Coproducts, will only resolve if all types in a Coproduct has an EncodeJson in scope
*/
object CoproductToArgonautFolder extends Poly1 {
/* Actions.scala */
package actions
trait Action // Trait ~= abstract class / interface. Can't be directly instantiated.
sealed trait Feed extends Actions // Sealing is not required but important as the compiler will know that this class can't be extended by any other code, meaning we can do static typechecking that anything that is supposed to handle all cases of Feed actually does so. More reading: http://underscore.io/blog/posts/2015/06/02/everything-about-sealed.html
/* Objects are often used similar to packages in Scala. In this case you can just think of them as a grouping, no other special meaning */
object Feed {
class FetchRequest extends Feed
function generator(baseType) {
// Need to handle variable length argument lists
const fun = (payload) => ({
type: fun,
payload: payload,
baseType: baseType
});
return fun;
}
// lib/actions.js
import identity from 'lodash/identity';
export const ActionType = {
REQUEST: 'REQUEST',
SUCCESS: 'SUCCESS',
FAILURE: 'FAILURE'
};
function createActionCreatorFlat(prefix, type, requestPayloadCreator = identity) {
@salanki
salanki / activerecord.md
Last active February 7, 2018 06:52
Performance effects of subtly different ActiveRecord methods

Checking for existance

TL;DR

Use exists?

exits?

Ruby: