Couchbase queries using composite keys in F# - f#

How would one translate the following composite key query:
?stale=false&connection_timeout=60000&limit=10&skip=0&startkey=["Default",{}]&endkey=["Default"]&descending=true
to couchbase .net api when using F#. I found a similar using C# LINQ here
Couchbase .Net Library complex startKey/endKey types, but how can I accomplish the same using F#?
The missing parts are the ???
let result = myView.Descending(true).Stale(StaleMode.False).Limit(limit).StartKey( ??? ).EndKey( ??? )
Any help would be appreciated.

It appears that you're asking how to create an array in F#. To declare an object array in F# do this:
let (startKey: Object array) = [|35; 23; new Object()|]
let (endKey: Object array) = [|35; 23|]
Note that normally the type specification isn't needed, but since you're mixing types in the array, the compiler will assume the type of the first object in the array (int) and so the new Object() would cause a compile error. Adding the type specification fixes that issue.
let result = myView.Descending(true).Stale(StaleMode.False).Limit(limit).StartKey( startKey ).EndKey( endKey )

Related

How to use Akka.Streams.*.ConcatMany in F#?

I want to create a flow that creates a new source (it will be a persistence query) out of incoming elements, and then flattens the results. Something like this simplified example:
var z = Source.Single(1).ConcatMany(i => Source.Single(i));
this code compiles and works as expected. My problem is that when I translate it to F#:
let z = Source.Single(1).ConcatMany(fun i -> Source.Single(i))
I get an error saying
This expression was expected to have type
'IGraph<SourceShape<'a>,Akka.NotUsed>'
but here has type
'Source<int,Akka.NotUsed>'
I think that the cause of that is that F# handles co/contravariance differently than C# and cannot simply convert these generic specializations (https://github.com/fsharp/fslang-suggestions/issues/162), but I cannot figure out a way to make a convertion between an int and a SourceShape<int>. Is it possible to convert this example to F#?
Looking at the code on GitHub, it appears that Source<TOut, TMat> is a direct implementation of IGraph, so you should just be able to cast it:
public sealed class Source<TOut, TMat> : IFlow<TOut, TMat>, IGraph<SourceShape<TOut>, TMat>
let z = Source.Single(1).ConcatMany(fun i -> Source.Single(i) :> IGraph<SourceShape<int>,Akka.NotUsed>)
I think the biggest difference between the C# and F# usage is that C# will automatically do the upcast for you.
One workaround that I found is to use Akkling.Streams wrapper library:
open Akkling.Streams
let x =
Source.singleton 1
|> Source.collectMap(fun x -> Source.singleton x)
the question how to do this without Akkling remains open.

add dictionary to dictionary in swift

according to this page it is possible to add an entire dictionary to another
http://code.tutsplus.com/tutorials/an-introduction-to-swift-part-1--cms-21389
but running the code gave me compilation error
var dictionary = ["cat": 2,"dog":4,"snake":8]; // mutable dictionary
dictionary["lion"] = 7; // add element to dictionary
dictionary += ["bear":1,"mouse":6]; // add dictionary to dictionary
error :
[string: Int] is not identical to UInt8
is there a right way to do this functionality in swift ?
of i should add them 1 by 1 ?
The page you are referring to is wrong, += is not a valid operator for a dictionary, although it is for arrays. If you'd like to see all the defined += operators, you can write import Swift at the top of your playground and command+click on Swift, then search for +=. This will take you to the file where all of the major Swift types and functions are defined.
The page you linked to also includes some other erroneous information on quick glance in the array section where it says you can do this: array += "four". So, don't trust this page too much. I believe you used to be able to append elements like this to an array in earlier versions of Swift, but it was changed.
The good news is that with Swift you can define your own custom operators! The following is quick implementation that should do what you want.
func +=<U,T>(inout lhs: [U:T], rhs: [U:T]) {
for (key, value) in rhs {
lhs[key] = value
}
}
Almost invariably when swift complains something is not like UInt8, there's a casting error in your code that may not be obvious, especially in a complex expression.
The problem in this case is that the + and += operators are not defined for that data type. A very nifty way to join arrays is described here:
How do you add a Dictionary of items into another Dictionary

Can I extend built-in array's indexer?

I want to add my own indexer to generic array type:
type 'T``[]`` with
member this.Item(x: string) = 1 // test
However, this gives a compile error "expression was expected to have type int but here has type string (FS0001)."
let a = [|1|]
let b = a.["aa"] // error: FS0001
let c = a.Item("aa") // this works.
I found this question which was asked 3 years ago and the answer suggested it's an FSharp bug. Want to confirm if it is still the case, or whether the specification has been updated since then?
I got this question answered by Don Syme here:
The specification needs to be clarified that indexers for arrays may not be extended.
I'd say the suggestion to allow them is best tracked via http://fslang.uservoice.com. That said, it is not simple to do this, because array indexers are "built in" to the compiler and have no F# or IL metadata representation.
So it's not supported in Fsharp.

FSharp.Data.JsonProvider - Getting json from types

The FSharp.Data.JsonProvider provides a means to go from json to an F# type. Is it possible to go in the reverse direction i.e. declare an instance of one of the types created by FSharp.Data.JsonProvider, set the field values to be what I need, and then get the equivalent json?
I've tried things like this,
type Simple = JsonProvider<""" { "name":"John", "age":94 } """>
let fred = Simple(
Age = 5, // no argument or settable property 'Age'
Name = "Fred")
The latest version of F# Data now supports this. See the last example in http://fsharp.github.io/FSharp.Data/library/JsonProvider.html.
Your example would be:
type Simple = JsonProvider<""" { "name":"John", "age":94 } """>
let fred = Simple.Root(age = 5, name = "Fred")
This is one area where C# has an edge over F#, at least in Visual Studio. You can copy your JSON example code into the clipboard and in Visual Studio use the Edit -> Paste Special -> Paste JSON As Classes and it will create a class to match the JSON example. From there you can easily use the class in F#.
More details on paste special here
Hopefully a matching feature will come for F# soon too.

OGNL Hello World in Java

I need to use OGNL for reading some properties from Java object. OGNL is completely new thing to me. The documentation available for OGNL is OGNL's website is really confusing to me.
So anyone can provide a simple HelloWorld example for using OGNL (or any link to a tutorial is also helpful).
Try this:
Dimension d = new Dimension(2,2);
String expressionString = "width";
Object expr = Ognl.parseExpression(expressionString);
OgnlContext ctx = new OgnlContext();
Object value = Ognl.getValue(expr, ctx, d);
System.out.println("Value: " + value);
If the intention is only to read properties from an object then PropertyUtils.getProperty (from commons-beanutils) may suffice. However, if the intention is to evaluate conditionals and such, then Ognl may benefit.
Here is the same Dimension example with a boolean:
Dimension d = new Dimension();
d.setSize(100,200) ;// width and height
Map<String,Object> map = new HashMap<String,Object>();
map.put("dimension", d);
String expression = "dimension.width == 100 && dimension.height == 200";
Object exp = Ognl.parseExpression(expression);
Boolean b = (Boolean) Ognl.getValue(exp,map);
// b would evaluate to true in this case
OGNL allows you to access objects fields and methods via string expressions which becomes very useful when you have lose coupled architecture between data and it's consumers. It's using reflection under the hood but definitely speeds up development compared to a pure reflection approach.
Some one line examples
System.out.println(Ognl.getValue("x", new Point(5,5)));
System.out.println(Ognl.getValue("size", new ArrayList<Object>()));
Documentation already has a number of basic and more advanced ognl expressions.
Here is an example helloworld for jython (python that compiles to java).
from ognl import Ognl, OgnlContext
from java.lang import String
exp = Ognl.parseExpression("substring(2, 5)")
print Ognl.getValue(exp, OgnlContext(), String("abcdefghj"))

Resources