FAQ Database Discussion Community


Fancy File Slurping in Perl

perl,file-io,scope,syntactic-sugar,deobfuscation
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...

Haskell stripping function of syntactic sugar

haskell,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...

How to determine the corresponding language of a grammar?

semantics,context-free-grammar,syntactic-sugar
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?

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

f#,delegates,syntactic-sugar
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...

Go - constructing struct/json on the fly

python,json,go,syntactic-sugar
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...

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

c#,.net,arrays,list,syntactic-sugar
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...

Stripping the newtype constructor

haskell,constructor,strip,syntactic-sugar,newtype
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...

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

c#,asp.net-mvc-6,syntactic-sugar
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?...

Unifying lookahead and lookbehind into a single regex operator

.net,regex,syntactic-sugar,negative-lookahead,negative-lookbehind
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

ruby-on-rails,ruby,if-statement,syntactic-sugar
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 ...