Validation in F#
The full code for this article is up on GitHub.
I like to the keep things as simple as possible and, ideally, reusable. At my previous employment I came up with a method of validation, but I was never entirely happy with the results. Some of the code repeated itself, and it was difficult to extend to arrays and array comparison. So, I went back to the drawing board building on top of the ideas that I created with the first validation library I created.
Swagger is too Restrictive
Swagger has a definite idea of what makes a good URL. But if you want to do anything outside of that good luck. I think, that even though it seems like a good idea to push people towards a standard it doesn’t allow the user to really create RESTful APIs which suit their own needs.
Let’s say that you have a reports resource. Sometimes you would like to get the JSON data of the report. Sometimes you would like a partial static HTML page of the report. Well, with RESTful APIs you could have something like:
JavaScript-Style Promises in F#
Working in enterprise level back end software often times I need to get
information from many different sources. When you have enough of these sources
the time it takes to get all the information can really add up. That’s when F#
async
work flow really comes in handy. Except, there is a gotcha.
Let’s say we have two sources we need to fetch data from:
let myData () = async {
let! a = myData1 ()
let! b = myData2 ()
let resultA = a
let resultB = b
// ... do stuff
// return result
}
So, when you look at that it looks like it should fetch the two data sources at the same time. Well, it doesn’t. It does it one at a time, serially. It just doesn’t block the thread while it is doing the fetching for each asynchronous call.
Using NPM as a Build Tool
I really like simplicity. The tools I use, I like them to make my life easy, not hard. They need to work out of the box. No huge configuration settings, just make it easy so I can get to work.
Well, along come grunt
, gulp
, and now even more tools since I first started
writing this that I won’t mention. Two build tools for JavaScript. Wonderful,
we need tools to help us build our files. Wait a minute. We already have a
build tool, it’s called NPM. By the time I started learning JavaScript,
gulp.js
was the cool thing. I think gulpl.js
is a really nice tool. But, if
you are building simple apps or even complex apps you can get away with just
the command line and keep things simple and concise.
On-the-Fly Lambda Expressions in JavaScript
Before many of the JavaScript compilers became real popular and if you wanted to write in straight JavaScript you could create functions on the fly. This was really nice for doing lambda functions.
Here’s a couple libraries that did this.
https://github.com/fschaefer/Lambda.js https://github.com/dfellis/lambda-js
Here’s how it basically worked:
var f = function(func){
var funcArray = func.split('->')
return (funcArray.length === 1)
? new Function('x', 'return (' + funcArray[0].trim() + ')')
: new Function(funcArray[0].trim(), 'return (' + funcArray[1].trim() + ')')
}
Pretty simple, but definitely not best practice now days.
Building a Convention Based Routes in Node.js
Convention over configuration (also known as coding by convention) is a software design paradigm which seeks to decrease the number of decisions that developers need to make, gaining simplicity, and not necessarily losing flexibility.
It seems in programming, if we haven’t learned the basic practices of others, we’ll end of repeating those practices; discovering them on our own. This isn’t necessarily a bad thing. It can be a good way to learn. But at the same time, we can learn faster just by learning from others first. I also like to think about how a pattern or architecture could come about.
CSS Media Object
I just finished attending the RockIt Bootcamp (12 week program). It’s a program that teaches the LAMP full stack development (Linux, Apache, mySQL, PHP). In the next little while I will be going over some of the things which I learned.
The css media object was first coined by Nicole Sullivan in 2010. It is still one of the most basic constructs to learn and understand in created good CSS content.
Angular.js vs React.js vs Mithril.js
Apparently Facebook has a bunch of functional-style programmers, not writing in ClosureScript though! https://www.youtube.com/watch?v=IVvHPPcl2TM
http://youtu.be/nYkdrAPrdcw?t=40m30s
[Starts at 40 minute 30 seconds. Person asks question then Jing Chen laughs at question, she must know how much Pete doesn’t like Angular.] So, the question was comparing and contrasting…React plus Flux and Angular. …[T]hey do solve some of the same problems but they go at it in very different ways. So React is focused a lot on treating your code as a black box. So, there’s no sort of observable abstraction inside of react you simply say, “Hey, rerender the UI and you present a consistent view of what you want your UI to look like.” With Angular you are basically passing data throughout these things called scopes which observe your data model and I think… that’s a very leaky abstraction. It forces you to compose your application not in terms of functions and objects but in terms of directives and model-view-controller and their flavor of model-view-controller. So, while it does work for a certain class of applications as you scale up you start to miss the past 40 or 50 years of research and how to abstract a program. So if you… push that kind of data binding concern out to the edges of your system like React does I think it leads to faster iteration time.
– Pete Hunt - Engineering Manager, Rethinking Web App Development at Facebook, Facebook
bilby.js & lenses
In JavaScript nearly everything is mutable. This can cause problems in your code when you think you have a new object or variable but instead you are operating on the referenced object. So, we create patterns to alleviate this problem. Or we use libraries like underscore.js or lodash.js which incorporate the functional concepts. Unfortunately they don’t always use immutable objects either.
Bilby.js solves the mutability problem by using lenses. Using the lenses pattern one can access and change one’s objects in a safe and immutable manner.
Match Expression for JavaScript?
Sometimes (or rather most of the time) it would be nice to have more concise code in JavaScript. I was working with a function that needed quite a bit of if
statements. if
was awfully cluttered. I had looked at bilby.js examples and hadn’t realized that you can do multiple ternary expressions
in a row.
So, instead of,
if (0){
return 0
}
else if (1){
if (2) {
return 1
}
else if (3) {
return 2
}
else {
return 3
}
}
else {
return 4
}
You can do,