by dmitriid · 6y ago · 0 comments
And coming up against arbitrary three-four-character combinations people come up with for their operators.
Terser does not automatically mean better (or J and K would've won that argument a long time ago).
At least mainstream languages are usually consistent. A method call is a method call. A structure is a structure. What is this ungodly mess?
type API = "polls" :> Get '[JSON] [Poll]
:<|> "polls" :> Capture "question_id" Int :> Get '[JSON] Poll
:<|> "polls" :> Capture "question_id" Int :> "results" :> Get '[JSON] PollResults type API = "polls" :> Get '[JSON] [Poll]
:<|> "polls" :> Capture "question_id" Int :> Get '[JSON] Poll
:<|> "polls" :> Capture "question_id" Int :> "results" :> Get '[JSON] PollResults
Delightful :D The API is the route "/polls" accessed via GET, returning a list of Polls in JSON format
or the route "/polls/question_id" where question_id is an Int, accessed via GET, returning a Poll in JSON format
or the route "/polls/question_id/results" where question_id is an Int, accessed via GET, returning PollResults in JSON formatCould we leave out this tick given that the kind of this parameter is [*]?
[JSON] and '[JSON] have different meanings. Here, '[JSON] does not refer to a list of json files, but to a type-level list of the mimetypes that can be returned. You could see '[JSON, PNG] instead, which would make no sense in the context of a list type declaration.
The genius of Servant is that the DSL doesn't require you to know how '[] is implemented and why it's different from [], you just have to know the DSL.
Nothing requires that though, it's more of a debatable community habit, similar to the overly abstract or cutesy names.
> At least mainstream languages are usually consistent. A method call is a method call. A structure is a structure. What is this ungodly mess?
Haskell is arguably way more consistent there: "mainstream languages" also have operators, often overloadable, you just can't declare your own operators in most of them.
Haskell simply allows sigils to be used as infix functions, aka binary operators, and furthermore allows those to be used as prefix functions, as well as prefix binary functions to be used infix.
An eDSL ? Much like every DSL, it requires to invest some time learning, and gives you something if you take that time. It's not really specific to Haskell.
Back when I was doing some Java, I had to learn the logic of maven xml files, and it was exactly the same kind of investment-for-future-reward.
/polls returns an array of Poll. /polls/:question_id returns a Poll. /polls/:question_id/results returns a PollResults.
the thing you call a mess is the combination of type level operators which helps you describe your API which is the essence of functional programming.
It is telling that this is "just a typed api with 3 routes". Somebody who haven't learned all the specific bits of Haskell (or, for that matter, specific bits of this program that may be overloading strange operators to define routes) won't even start to guess that it's defining routes. They can guess it is defining an API, thanks to the words used at the beginning of the snippet. And even though you told us that it's defining a route, I cannot figure out what it is really doing. It is just not clear.
So, yes, it looks like a mess for the foreign eye. I have no doubt it looks straightforward as soon as you learn Haskell and practice a bit.
Mainstream languages also look gibberish for people who haven't learned programming though. That's also something that we have to humbly keep in mind while, as foreigners who practice other programming languages, we discuss the syntax of Haskell.
it does nothing. :) that code sample is a description of the API. nothing more. when you're writing programs in a functional way, you describe your computation rather than actually computing it as opposed to imperative languages. imagine summation in math. it is a description rather than computation. you compute the sum according to the description. this API description is exactly the same.
the code in the comment is incomplete which makes it harder for you to figure out. where are the handlers? how do we return responses? i believe the rest of the code is actually mapping the API description to actual handlers.
server :: Server API
server = getPolls :<|> getPoll :<|> getResults
getPolls = queryDBForPolls `andThen` transFormToJson
getPoll pollId = (queryDBForPoll pollId) `andThen` transformToJson
getResults pollId = (queryDBForResults pollId) `andThen` transformToJson
i agree with the comments and sentiments which say mathematicians learn and write haskell easier than the rest of us but i will tell you a secret: i'm a high school graduate with no degree at all let alone a math / cs one. i'm using haskell happily but it took way much longer time than probably everybody else. it just takes a bit of focused time and will to learn it.It also looks nothing like what programmer might encounter earlier when learning programming languages.
Actually I think Haskell seems to be a little bit similar to mathematical notations so it might be easier for mathematicians to learn than programmers. Unfortunately they don't really have a business case for knowing Haskell and they learn Python instead.
But they have to keep in mind that this verbosity is a feature and they should not act surprised that many people like it. This verbosity makes many things explicit, and I'll not be the first to think that "Explicit is better than implicit".
Conciseness is also a feature, though.