Running a quick clone of riemann locally is great for practice.
git clone riemann
change directory to the directory where riemann is.
make sure you lein
installed.
lein run
in another console tab run
lein repl :connect localhost:5557
In the directory you have cloned riemann into edit riemann.config
and add just below ; vim: filetype=clojure
(load-plugins)
Now start riemann again with lein run
and see that the plugin is loaded in console.
akka spray tomcat example in scala
Lets say you have the following case class (important its a case class for regular classes you will need something a little different)
case class Person(val name: String)
and you want json-spray to serialize it you will need the define the following implicit you can just define it beside your case class in the same file.
object PersonJsonImplicits extends DefaultJsonProtocol {
implicit val impPerson = jsonFormat1(Person)
}
Then you can use toJson on your pimped class:
** You must import spray.json._ otherwise toJson
won’t be recognized! **
import spray.json._
new Person("somename").toJson
note the 1
in jsonFormat1
this means your case class has single parameter if it had 2 you would use… jsonFormat2
:)
Remember: spray uses a single actor to handle incoming requests. This means you cannot run any blocking code in the handling actor.
// In spray main request handler actor.,
complete {
// NO BLOCKING CODE HERE
}
So you have two options (or 3).
1. Route the request to another actor handle it there, use ask ?
pattern to get back a future. complete {}
will then know to handle the Future
. (nothing special required).
2. Use directly a future - I like that one better its more clear.
complete {
Future {
Thread.sleep(5000) // blocking example.
"tada.."
}
}
you may need to define above the implicit executionContext
that the Future
will run in and also the implicit timeout
detach
now if you want to handle your request in an async way you don’t really need to create actors
spray can do that for you just wrap your handling with detach {
as following:
detach() { // this make the below operation async (note for your app to really be async you should not block the underlying thread!)
respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default, so we simply override here
complete {
<html>
Note that although we called detach()
if you have inside the code which detach calls some blocking code
this would still mean it would block the thread on which detach is running therefore if you do any blocking get
you should
still wrap it in a future and have a callback for it (same as you should not block from within handling of a message in an actor).
source code available at: github source code
Have you been finding yourself lately coding complex logic for deciding when to send an alert
? If yes riemann
is for you. You can define cewl DSL
s for CEP
logic for when to send an alert (or whatever else).
Embeeding riemann with an example:
<dependency>
<groupId>riemann</groupId>
<artifactId>riemann</artifactId>
<version>0.2.6</version>
</dependency>
example riemann.conf
; -*- mode: clojure; -*-
; vim: filetype=clojure
(logging/init {:file "riemann.log"})
; Listen on the local interface over TCP (5555), UDP (5555), and websockets
; (5556)
(let [host "127.0.0.1"]
(tcp-server {:host host})
(udp-server {:host host})
(ws-server {:host host}))
; Expire old events from the index every 5 seconds.
(periodically-expire 5)
(let [index (index)]
; Inbound events will be passed to these streams:
(streams
(default :ttl 60
; Index all events immediately.
index
; Log expired events.
(expired
(fn [event] (info "expired" event))))))
public static void main(String[] args) throws Exception {
riemann.bin.main(new String[]{"riemann.config"});
}
RiemannClient c = RiemannClient.tcp("localhost", 5555);
c.connect();
c.event().
service("fridge").
state("running").
metric(new Random().nextDouble()).
tags("appliance", "cold").
send();
c.query("tagged \"cold\" and metric > 0"); // => List<Event>;
c.disconnect();
I don’t want to clutter my local environment with it so i’ll just use docker
to install riemann-dash
sudo docker run -p 4567:4567 davidkelley/riemann-dash
Unable to find image 'davidkelley/riemann-dash' locally
Pulling repository davidkelley/riemann-dash
7911459329f4: Download complete
b2cec74ebcfd: Download complete
b56feebb5913: Download complete
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Thin
Then navigate to: http://localhost:4567/
next this is a little tricky. You will see a big Riemann
word on your screen. Click it. Then click e
this will edit it. Choose Gauge
in the textbox type true
this means your query filter is for everything. Next use the client to send some more data you will see this metric changes online realtime with your data!
source code available at: github source code
Have you been finding yourself lately coding complex logic for deciding when to send an alert
? If yes riemann
is for you. You can define cewl DSL
s for CEP
logic for when to send an alert (or whatever else).
Embedding riemann with an example:
<dependency>
<groupId>riemann</groupId>
<artifactId>riemann</artifactId>
<version>0.2.6</version>
</dependency>
example riemann.conf
; -*- mode: clojure; -*-
; vim: filetype=clojure
(logging/init {:file "riemann.log"})
; Listen on the local interface over TCP (5555), UDP (5555), and websockets
; (5556)
(let [host "127.0.0.1"]
(tcp-server {:host host})
(udp-server {:host host})
(ws-server {:host host}))
; Expire old events from the index every 5 seconds.
(periodically-expire 5)
(let [index (index)]
; Inbound events will be passed to these streams:
(streams
(default :ttl 60
; Index all events immediately.
index
; Log expired events.
(expired
(fn [event] (info "expired" event))))))
public static void main(String[] args) throws Exception {
riemann.bin.main(new String[]{"riemann.config"});
}
RiemannClient c = RiemannClient.tcp("localhost", 5555);
c.connect();
c.event().
service("fridge").
state("running").
metric(new Random().nextDouble()).
tags("appliance", "cold").
send();
c.query("tagged \"cold\" and metric > 0"); // => List<Event>;
c.disconnect();
I don’t want to clutter my local environment with it so i’ll just use docker
to install riemann-dash
sudo docker run -p 4567:4567 davidkelley/riemann-dash
Unable to find image 'davidkelley/riemann-dash' locally
Pulling repository davidkelley/riemann-dash
7911459329f4: Download complete
b2cec74ebcfd: Download complete
b56feebb5913: Download complete
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Thin
Then navigate to: http://localhost:4567/
next this is a little tricky. You will see a big Riemann
word on your screen. Click it. Then click e
this will edit it. Choose Gauge
in the textbox type true
this means your query filter is for everything. Next use the client to send some more data you will see this metric changes online realtime with your data!
source code available at: github source code
Scala map and flatMap are surely mixing map, I mean mixing map, I mean mixing up.
Let’s start simple; All of those map
, flatMap
are methods of some classes
(for the sake of explanation classes, interfaces, whatever). So they all work on some kind of an encapsulation. map
and flatMap
are methods of Option
, of List
, of Future
.
If you look at map
and flatMap
method signatures you will see:
// for `Future`
def map[S](f: (T) ⇒ S)(implicit executor: ExecutionContext): Future[S]
def flatMap[S](f: (T) ⇒ Future[S])(implicit executor: ExecutionContext): Future[S]
// for `List`
def map[B](f: (A) ⇒ B): List[B]
def flatMap[B](f: (A) ⇒ GenTraversableOnce[B]): List[B]
// and for `Option`
def map[B](f: (A) ⇒ B): Option[B]
def flatMap[B](f: (A) ⇒ Option[B]): Option[B]
Hello, so yo wanna log a failure for a Future
in a less intrusive way than pattern-matching
and in a more elegant way in scala but don’t know how? Read below…
Pattern matching is good, but, its for newbies. If you are a serious scala developer you should use the more advanced fp
programming tools provided. For example - for
:)
But, you may ask, when I use the plain fors
I loose the ability to do something with the failures, and, yes, its not straight forward, and indeed when using for comprehensionn
its not immediate to get the failure, but only successes. Fortunately the previous sentence is incorrect, and for comprehension
in scala provides you a way to get the failure. So here is an example of how to use for
doing something with the successful
value and printing to log the errornous value.
import scala.util._
import scala.concurrent._
for {
futureResult <- Try(Future[MyResponse](doSomeOp())) // get back the value of the try which is a future.
} yield {
futureResult // if future is successful we get to here, and we return the future result.
} recover {
case e => LOG.warn(e.getMessage) // in case the future has failed of an error we get to recover and we print the exception. vwalla!
}
doSomeOp(): Future[String] = future("hello from the future")
Note that we did not check here whether the Try
was successfull
or not. So this piece of code might be a little misleading as you might think that the recover
checks for the successfullness
of the Try
which its not its checking the successfullness of the Future
as doSomeOp
returns a Future
.
You can use scalaz ValidationNel
(non empty list guaranteed) to hold one of two values, success
or failure
one. Basically you will have two types involved.
The success type
.
The failure type
.
The success type will be the actual type you return, the one you want.
The failure type can be String
with a message (this is what most of examples you will encounter will show), but in real life ofcourse you would not want just a String
but a type which will hold various stuff about your error, like error type from enum maybe and more details on error, could be even the stacktrace for better debugging, and a message ofcourse.
So lets say our actual type is Produce
(the success type). And our failure type (i’m falling into this like the others) is String
.
When you return something from your method you will use successNel
for the successful type and (you guessed) failureNel
for the failure type. The part which might mix you up (see below example so will be clarified) is that when you return the successful type you use successNel[FailureType] - thats right you specify the failureType because the success type is already in there. And when you return the failureType you use failNel[SuccessType] not intuitive at all! but it works.
import scalaz._
import Scalaz._
def myMethod: ValidationNel[String, RealResult] = {
parsedVal = parseIt()
parsedVal match {
case Success(s) => s.successNel[String] // String is our failure type - bah that's the way it is!
case Failure(e) => e.getMessage.failNel[RealResult] // bah! passing in type of real result on failure while failure is a string.
}
}
Now the question is, how do you parse back the results from validation? I mean so you did the validation and you have a validation object returned back. What do you do with it?
Good question, for one case you can match on the result as following:
import scalaz.{Success => SuccessZ, Failure => FailureZ}
validationResult match {
case SuccessZ(r) => println (s"hip hip $r hurrah!")
case FailureZ(e) => println (s"sh*t sh*t $e s***ty")
}
That’s all for today. And if you want to repeat the mantra after all then, repeat after me:
ValidationNel, ValidationNel, ValidationNel, ValidationNel, ValidationNel, ValidationNel, ValidationNel, ValidationNel, ValidationNel, ValidationNel, ValidationNel, ValidationNel, ValidationNel.
Hello, so yo wanna do some android development and is bombed by intents
, activities
, services
etc? Lets tidy up the stuff in our head a bissale. You need to understand there are a very few concepts you have to understand in order to start android development. These concepts are very few but you must first understand them, otherwise you get lost. So lets get done with it.
First thing you will notice is that the following components are very decoupled. I mean the next concepts solve a problem, how to build an android app (they can also solve a generic problem on how to build an app), but they are customized for mobile apps. So when you ask yourself why do I need an intent when I can just do something else instead, you must understand that its very nice to have an android app decoulped like that becuase it helps in many ways, instead of sending a message inside your app you use an intent
and in the same way you can send an intent to take a picture so its a common ground the language of the mobile.
paused
for example is when its like you long click the mid button and thus you see your app partially on screen, this is the pause
state.intent
for every button click, but if it should trigger another app showing up like contacts book
then this is the way to notify the android app manager this is what you wish to do.Activity
however without UI. Services do not run on their own thread they run in your own UI thread, that means if you should be aware of it and you should not run long running processes in it. If you do need your service to run in its different thread you should be using worker threads
system booted up
note that there is no notion in android for a service which starts up in startup and the OS
will be responsible for it being up, you should do that by means of listening to broadcase system booted up and start up your service (and handle its failures).widget
is i don’t need to explain to you, what you need to be aware of is that programming model is very simple component much similar to broadcasts its listening to events and reacting.In addition you should be aware that every app has its own process, its own linux user
is practically created for it (app_id123).
If you are like me and you have some java background then you like your projects structured in this way:
mynice-project-api
mynice-project-impl
myothernice-project-api
myothernice-project-impl
Because in this project construction methodology you have a clear separation between your api’s and implementations. Or
so to say you can use your favorite build tool to have your project compile against the api
and run with the impl
.
And for the bored programmers of our time you can event invest the time to replace your impl1
by impl-mock
. However,
I’m taking you as a serious developer transitioning into a more fp
language, like scala
.
However if you have transitioned to scala
. So in scala we are very used to having traits
everywhere, and hell, i mean
everywhere. Not a single impl
without an interface
. However if you noticed then in 99% of cases the traits are in same
files
as the implementations. Not to mentioned you have the sealed
option which will force you to have it in same file.
And thus the -api
-impl
is gone. So lets acknowledge it at least.
We have disregarded the sepration between the -api
and -impl
because we use traits all over the place. And you know what?
So far its working well for us, i mean, any serious programmer will enforce coding to an interface, so this separation of projects
is actually redundant for us, we even got half the amount of projects to maintain. Seriously how many times did you really replace that
impl
project with another one? I’ve seen hundreds of projects with that seperation and in none of them it was actually replaced. so
lets at least acknowledge it. And with the new programming paradigm, its so easy to create interfaces
we do it all over, and we don’t need
another project to convince us we need to create such a new interface.
And you know what? if you really really need such separation in a specific place go ahead, but no reason to have it all over the place cluttering our project workspace.
So you want to partially apply a function but don’t know why? This post is for you. Here we will show you why you did need to partially apply a function (why it helped you). And how nice and easy this process can be.
Although in scala the process for partially applying a function is just as complex and as encrypted as far as they could think of we will try to make it easy for you. (they really thought hard how to make it encrypted and un-remembable).
So let’s dig in.
But first I would like to ask you to repeat the mantra:
partial-applied-function, partial-applied-function, partial-applied-function, partial-applied-function, partial-applied-function, partial-applied-function, partial-applied-function, partial-applied-function, partial-applied-function.
Now that you are familiar with it let’s repeat the scala partially applied function mantra.
oh-lord-its-with-space-and-underscore, oh-lord-its-with-space-and-underscore, oh-lord-its-with-space-and-underscore, oh-lord-its-with-space-and-underscore, oh-lord-its-with-space-and-underscore, oh-lord-its-with-space-and-underscore, oh-lord-its-with-space-and-underscore, oh-lord-its-with-space-and-underscore, oh-lord-its-with-space-and-underscore_.
Let’s continue to dig in.
Let’s say you want to load some configuration from disk and lets say this configuration is in the form of properties. You wish to have a primary file and a secondary, like external configuration file which will override internal. (For real life if you use java I would recommend you to use already existing mechanisms such as spring who would do that for you or scala typesafe config manager which would also do that for you). But for the sake of the example lets use plan properties.
Lets first load the configurations into our properties.
val internalProps = new Properties()
val externalProps = new Properties();
extenralProps.load(new FileInputStream(new File("external.properties")))
defaultProps.load(new FileInputStream(new File("default.properties")))
Now lets load a properties with default - no partially applied func here.
val someValFromConf = externalProperties.get("key", defaultProperties.get("key"))
Now let’s partially apply props to already contain the internal defaults
def getOrDefault(externalProps: Properties, defaultProps: Properties)(key: String) = props.externalProps.getProperty(key, props.defaultProps.getProperty(key))
val get = getOrDefault(externalProps, defaultProps) _ // nasty underscore --> partially applying alarm.
Well here we first defined a method named getOrDefault which is aware of both external and default properties, so far so good, I mean, we are talking here about File, FileInputStream, Properties, def, all is clear and nice and dandy isn’t it???
AND HERE COMES THE TWIST IN THE STORY!! THERE IS AN UNDERSCORE!
The story of a method called only with some of its arguments
getOrDefault accepts three arguments, externalProps and defaultProps, and a key, what would happen if we called it with only a single argument? it would not compile right? but we added THE UNDERSCORE.
So as we added the underscore scala understands it should PARTIALLY APPLY meaning it calls them method with a single argument and as we have an _ it does not run it. Also note we are assigning the return value to a val so we have now got a new method called get and as we already passed to its origin method the first argument, we can call get only with the key.
so we can call key method any number of times we want, isn’t that nice, its already aware of the first two arguments. That’s the functional way of doing so. If we wanted to do so in java we would need to create an object initialize it with some members and call it, here all got shorter, and immutable.
So now we can call get like this:
val value = get("key1") // low and behold, the function is already aware of the external,internal props.
val value = get("key2") // low and behold, the function is already aware of the external,internal props.
homo-dev-sapience has long ago gone beyond the software-development realm into others. for example releasing homo-sapience-sapience from the burden of eating - Soylent. Into aeronautics, medicine, physics and the rest of the world problems.
The way the homo-dev-sapience works, is, it identifies a problem, and then:
«««< HEAD In a few years homo-dev-sapience will finish resolving more and more of world most difficult problems unsolved by people proficient in its domain field. It’s percentage in population is growing higher and higher. We expect by a few years most of homo-sapience-sapience being extinct by homo-dev-sapience just as happened to homo-n-1 by homo-n, afterwards we can highly expect the dev-sapience to be extinct by the robot-sapience-sapience, its own creation this time. ======= In a few years homo-dev-sapiens will finish resolving more and more of world most difficult problems unsolved by people proficient in its domain field. It’s percentage in population is growing higher and higher. We expect by a few years most of homo-sapiens-sapiens being extinct by homo-dev-sapiens just as happened to homo-n-1 by homo-n, afterwards we can highly expect the dev-sapiens to be extinct by the robot-sapiens-sapiens, its own creation this time. »»»> 8287ada5f392e9467feb5acfc9fcf37acf8df7c1
This is the book we have been waiting for. By saying the book is a university level course I mean you have the do the exercises to understand it, so treat it as a course.
The good: It provides what you wanted and what it’s title is - “Functional programming in Scala - indeed.” The bad: No jokes, no real fun. I like books with jokes and fun, the author does a good job in describing the issue in a very clear way, but hey couldn’t they put some fun into the book? (kinda learn you a Haskell for a great good), its explaining everything yes i just wonder if it can be explained in a simpler way, I think it can be I saw a few lectures were it was explained in a simpler manner, but as the book goes such a long way its an excellent reference for functional programming in scala, it will dive into all subjects, consider stuff not explained before so its plain extended reference.
CHAPTER 1: Introduction to functional programming, an example of how he pushes out the printing action from the actual computation function.
CHAPTER 2: A very good job in describing functions. In currying, partial functions, how generics or polymorphic functions as they are named play a big role. Why most of framework functions are like someFunc(A, B, C). Does a real good job on this. It’s still an introduction to the whole subject, but overall, by the end of this chapter, you will get to know tail recursion, generics (or type parameters) currying, partial functions, how to compose functions, why its like solving equations and stuff like that.
CHAPTER 3: Lot of exercises in this chapter. You have to decide at this point if you are going to go through all exercises which will take a huge amount of time, but you will definitely profit from them they are great for getting familiar and understanding this hard material. Now as for the chapter, again, it does a good job explaining ADT, abstract data types. While is does not go a long way to describe this vague ADT theoretical concept, or the concept itself, it describes how you define such ADT in Scala. You then have to do some deduction in order to understand this generic ADT concept. Now as for the examples these are List, Tree. Basically in List you have some multiple constructors where you can create such lists, and do some operations on them like Cons. It does some work on describing how to keep the implementation efficient as you want, does it in tail recursion, and the implementations are clean. In general it also emphasizes on the concept that whenever you see some repeating code you should generalize it and in case of functional programming you do it with extracting the operations which look alike let it be add or multiplication and extract it into a function parameter ‘f’ which you pass into the function any local variables you would use go into arguments into that ‘f’.
CHAPTER 4: Handling errors: The author acclaims we are getting back to C style error handling which is return values instead of exceptiosn in java, however as we get much powerful tools to handle these “return codes” and to force callers to handle them, it appears to be the correct path. This item has bugged me a lot when I read in “It may be easy to jump to the conclusion that once we start using Option , it infects our entire code base” Now I was delighted to see the author has referenced, that, I wouldn’t expect that in a book really, usually books deals with the trivial examples or how can I call them the straight forward path and not what the extreme people would think about.
CHAPTER 5: Strictness and Laziness: Well in functional programming you focus on declarative code, so its best if you would not know when the stuff is evaluated, it helps streams (infinite) what is strict and non strict.
CHAPTER 6: Purely functional state: A random number generator as an example (it kina non pure has internal state mutates stuff after all every time you call its its just crazy returning you a different number) , so you’ll make a pure version of it, curious to know how? hmm well its simple, a bit of lie, but that’s how it goes, the API will just compute the next random number return you with new number and state you provide it in with the state here yes we are now declared as pure.
CHAPTER 7: How do you design FP parallel API in scala? how explicit is your API about allowing clients to control the forks and joins of the parallel computation? What are the trade offs of this API? The chapter aims to answer this. A challenging chapter, I had to digest it a couple of times before getting it. There was an effort to describe it in a clear way, thanks for the effort, result not so clear.
Hey! If you are new to functional programming or to scala here are a few tips.
You must understand that map, Future, Monad, flatMap are an inherent part of the system. They have tweaks and are unfortunately hard to understand. You must understand very well the mechanisms behind them. I will try to help you with that.
So
Repeat after me:
map, map, map, map, map, map, map, map, map, map, map, map, map, map, map, map, map, map, map
I hope you feel more comfortable with map now. Now that it’s part of your dictionary lets drill down into it.
Hey! You can use jarjar like this u
def intme = 2 * 2 / 2
var item = intme
Hey! After many months of hard work by Jekyll’s contributors, we’re excited to announce the first major release of the project in a long while. v1.0.0 is finally here! While the list of improvements and bug fixes is quite lengthy, here are the highlights (thanks to @benbalter for the examples and for compiling this list):
Take a look at the Upgrading page in the docs for more detailed information.