Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache an intermediate variable in an one-liner

Tags:

scala

Can I somehow cache the i.toString in this simple definition of function?

def palindrome(i: Int) = i.toString == i.toString.reverse

I want to keep this function simple, w/o a classic multi-line, brace-enclosed function..

like image 241
Parobay Avatar asked Apr 11 '13 18:04

Parobay


People also ask

What is intermediate cache?

An Intermediate Cache or Proxy cache is cache store that sits between the client and the server in the cache chain.

How many lines of cache are in a set?

That means each cache line contains 16 bytes. If the cache is 64Kbytes then 64Kbytes/16 = 4096 cache lines.

How do you cache a variable in Python?

Implementing a Cache Using a Python Dictionary You can use the article's URL as the key and its content as the value. Save this code to a caching.py file, install the requests library, then run the script: $ pip install requests $ python caching.py Getting article... Fetching article from server...


2 Answers

You could do:

def palindrome(i: Int) = ((s:String) => s == s.reverse)(i.toString)
like image 177
Marimuthu Madasamy Avatar answered Oct 12 '22 12:10

Marimuthu Madasamy


Well, Scala doesn't have a let statement like some traditional functional languages, but that's largely because val + braces fulfill the same purpose. Are you objecting to the multi-line part or to braces in general? Because it's pretty hard to beat:

def palindrome(i: Int) = { val s = i.toString; s == s.reverse }

Attempts to elide the braces will likely only drive the character count up.

like image 33
Reimer Behrends Avatar answered Oct 12 '22 14:10

Reimer Behrends