Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion any less verbose way to extract params ?

Received: by 10.58.154.102 with SMTP id vn6mr8290768veb.2.1351194843327;
        Thu, 25 Oct 2012 12:54:03 -0700 (PDT)
X-BeenThere: play-framework@googlegroups.com
Received: by 10.52.89.80 with SMTP id bm16ls4083994vdb.9.gmail; Thu, 25 Oct
 2012 12:53:56 -0700 (PDT)
Received: by 10.52.175.5 with SMTP id bw5mr3958646vdc.16.1351194836344;
        Thu, 25 Oct 2012 12:53:56 -0700 (PDT)
Date: Thu, 25 Oct 2012 12:53:54 -0700 (PDT)
From: peter hausel <peter.hau...@gmail.com>
To: play-framework@googlegroups.com
Message-Id: <2045dc94-b87a-470b-82a3-5df5dbdec597@googlegroups.com>
In-Reply-To: <CAKVWSxuQuNYkKsHY+gmgkTvZpv0sqto5Db4vZDcos-bJN+X=yw@mail.gmail.com>
References: <CAKVWSxuQuNYkKsHY+gmgkTvZpv0sqto5Db4vZDcos-bJN+X=yw@mail.gmail.com>
Subject: Re: [2.0.4 scala] any less verbose way to extract params ?
MIME-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_22_5723228.1351194834919"

------=_Part_22_5723228.1351194834919
Content-Type: multipart/alternative; 
	boundary="----=_Part_23_23295918.1351194834919"

------=_Part_23_23295918.1351194834919
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

what's wrong with using a simple function?

scala> def parseToInt(s: Option[String]): Int = {
     |  val intCatch = 
util.control.Exception.catching(classOf[NumberFormatException]).withApply(_ 
=> 0)
     |  s.map(s => intCatch(s.toInt)).getOrElse(0)
     | }
parseToInt: (s: Option[String])Int

scala> parseToInt(Some("1"))
res0: Int = 1

scala> parseToInt(Some("sdf"))
res1: Int = 0

scala> parseToInt(None)
res2: Int = 0

in your case: 

parseToInt(request.queryString.get("limit"))

On Thursday, October 25, 2012 3:31:15 PM UTC-4, RottenBits wrote:
>
> I am just trying to extract a numeric paging limit. But, this seems 
> unnecessarily verbose to me. Not to mention there is relatively some 
> sophisticated syntax here.
>
>   
>
>>   val limit = request.queryString.get("limit").flatMap{
>>       import scala.util.control.Exception._;
>>       catching(classOf[NumberFormatException]) opt _(0).toInt
>>     }.getOrElse(10)
>
>
>
> Some thoughts on what I would like it to look like.
>
> val limit = request.getFirstParam("limit").parseOptionInt.getOrElse(10).
>
>
> Of course, I could write a some implicit or utility code to do just that. 
> But, every time i am doing a quick prototype of an app, that would be 
> cumbersome to maintain and carry around.
>
> thanks
> Arun
>

------=_Part_23_23295918.1351194834919
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

what's wrong with using a simple function?<div><br></div><div><div>scala&gt=
; def parseToInt(s: Option[String]): Int =3D {</div><div>&nbsp; &nbsp; &nbs=
p;| &nbsp;val intCatch =3D util.control.Exception.catching(classOf[NumberFo=
rmatException]).withApply(_ =3D&gt; 0)</div><div>&nbsp; &nbsp; &nbsp;| &nbs=
p;s.map(s =3D&gt; intCatch(s.toInt)).getOrElse(0)</div><div>&nbsp; &nbsp; &=
nbsp;| }</div><div>parseToInt: (s: Option[String])Int</div><div><br></div><=
div>scala&gt;&nbsp;parseToInt(Some("1"))</div><div>res0: Int =3D 1</div><di=
v><br></div><div>scala&gt;&nbsp;parseToInt(Some("sdf"))</div><div>res1: Int=
 =3D 0</div><div><br></div><div>scala&gt;&nbsp;parseToInt(None)</div><div>r=
es2: Int =3D 0</div><div><br></div><div>in your case:&nbsp;</div><div><br><=
/div><div>parseToInt(request.queryString.get("<wbr>limit"))</div><br></div>=
<div>On Thursday, October 25, 2012 3:31:15 PM UTC-4, RottenBits wrote:<bloc=
kquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-l=
eft: 1px #ccc solid;padding-left: 1ex;"><div>I am just trying to extract a =
numeric paging limit. But, this seems unnecessarily verbose to me. Not to m=
ention there is relatively some sophisticated syntax here.</div><div><br></=
div><div>&nbsp;&nbsp;</div><blockquote class=3D"gmail_quote" style=3D"margi=
n:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204=
);border-left-style:solid;padding-left:1ex">
&nbsp; val limit =3D request.queryString.get("<wbr>limit").flatMap{<br>&nbs=
p; &nbsp; &nbsp; import scala.util.control.Exception._<wbr>;<br>&nbsp; &nbs=
p; &nbsp; catching(classOf[<wbr>NumberFormatException]) opt _(0).toInt<br>&=
nbsp; &nbsp; }.getOrElse(10)</blockquote><div><br>
</div><div><br></div><div>Some thoughts on what I would like it to look lik=
e.</div><div><br></div><blockquote class=3D"gmail_quote" style=3D"margin:0p=
x 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);bo=
rder-left-style:solid;padding-left:1ex">
val limit =3D request.getFirstParam("limit")<wbr>.parseOptionInt.getOrElse(=
10).</blockquote><div><br></div><div>Of course, I could write a some implic=
it or utility code to do just that. But, every time i am doing a quick prot=
otype of an app, that would be cumbersome to maintain and carry around.</di=
v>
<div><br></div><div>thanks</div><div>Arun</div>
</blockquote></div>
------=_Part_23_23295918.1351194834919--

------=_Part_22_5723228.1351194834919--