FAQ Database Discussion Community

Haskell stripping function of syntactic sugar

I'm wondering how would the following function look without any syntactic sugar: tails1 :: [a] -> [[a]] tails1 [] = [[]] tails1 xs@(x:xs') = xs:tails1 xs' I'm mostly concerned with the usage of the @ operator, I've tried what follows, but that's obviously not the correct way tails1 ((:) x...

Stripping the newtype constructor

More often than not I am writing functions that are stripping the only constructor of a new type, such as in the following function to return the first argument that is not Nothing: process (Pick xs) = (\(First x) -> x) . mconcat . map (First . process) $ xs...

Unifying lookahead and lookbehind into a single regex operator

I am making a simplified/sugary wrapper for regex which cuts out many of the more complicated regex functions (whilst still keeping the essentials for 99% of uses), and which also tries to tidy up the syntax a little. In regards to negative lookahead/lookbehind, I found it confusing why they can't...

Ruby convert expanded if statement into one-liner

Is there a nicer way to write this if/else statement, given that there’s only a tiny difference between the two parts? if current_admin_user.role?(:admin) f.input :invoice_file_date, as: :datepicker if f.object.invoice_file.present? else f.input :invoice_file_date, as: :datepicker, :input_html => { :disabled => true } if f.object.invoice_file.present? end ...

Sugar coated arrays (dynamically resizable and set any element at random)

I want my cake and to eat it. I like the way Lists in C# dynamically expand when you go beyond the initial capacity of the array. However this is not enough. I want to be able to do something like this: int[] n = new int[]; // Note how...

Invoking C# delegate from F# - Is there a cleaner syntax?

In C#, if a delegate is defined as follow: delegate void A(); A a = () => {Console.WriteLine("Test")}; It can be invoked with: a.Invoke(); // Works a(); // Also works In F#, the C# delegate can be invoked: a.Invoke() // Works fine. a() // Error: This value is not a...

How to determine the corresponding language of a grammar?

I have the following grammar that uses center embedded recursion. However, it has two cases using an or: S-> aSbbb | aSbb | ϵ where ϵ is an empty set. Is there a way to generate a comprehensive mathematical formula (Language) that determines that grammar?

Go - constructing struct/json on the fly

In Python it is possible to create a dictionary and serialize it as a JSON object like this: example = { "key1" : 123, "key2" : "value2" } js = json.dumps(example) Go is statically typed, so we have to declare the object schema first: type Example struct { Key1 int...

Syntactic sugar in Web Api - how to omit [FromBody]

Is it possible to omit writing [FromBody]? [HttpPost] public string SomeMethod([FromBody]SomeModel model) { return "OK"; } Should I apply some global attribute? How to achieve this?...

Fancy File Slurping in Perl

I was looking into efficient ways to read files in Perl and came across this very interesting one liner. my $text = do { local (@ARGV, $/) = $file; <> }; My question is how exactly does this work? Normally when slurping a file you set $/ = undef, but...