English 中文(简体)
Looping over db intmap(person)
原标题:
  • 时间:2011-07-11 12:05:18
  •  标签:
  • opa

example:

type person = { name : string
                ; age : int
              }

db /person : intmap(person)

I know how to get a single person from the db, but how do I get them all? and print them in a html table?

Thx.

最佳回答

I advise you to use Db.intmap_fold_range instead of InMap.fold. It will be faster than InMap.fold which need to build the all map in OPA before folding on it.

http://opalang.org/resources/doc/index.html#db.opa.html/!/value_stdlib.core.db.Db.intmap_fold_range

Here is an example for your type:

type person = { name : string
                ; age : int
              }

db /person : intmap(person)

add(name, age) =
  /person[age] <- { ~name; ~age }

fold_person(acc, id) =
 person = /person[id]
 <>{acc}</><li>{person.name} {person.age}</li>

start() =
  do add("name1", 1)
  do add("name2", 2)
  do add("name3", 3)
  Db.intmap_fold_range(
    @/person,
    fold_person,
    <></>, 0, none, /* acc, starting key, optional max */
    (_ -> true) /* check range */
  )

server = one_page_server("Hello", start)
问题回答

This should do it :

my_html_list() =
  List.map(
    person -> <li>{person.name}</li>,
    IntMap.To.val_list(/person)
  )

render() = <ul>{my_html_list()}</ul>

... and then call render() in your server page ...

Simply put:

  • /person[i] gets element i from the database
  • /person gets the whole intmap from the database
  • !/person gives you a handle to the database, on which you could use, for example, Db.intmap_fold_range for optimised partial access

Following @shomodj comment on my answer, this is my complete code to print the persons list (inspired by Cédrics code)

// Declare type person
type person = {
  name : string
  age : int
}

// Declare database
db /person : intmap(person)

// Add function
add(name, age) =
  /person[age] <- { ~name ~age }

// Add to database when server launches
do add("name1", 1)
do add("name2", 2)
do add("name3", 3)

// Build the persons <li> list
my_html_list() =
  List.map(
    person -> <li>{person.name}</li>,
    IntMap.To.val_list(/person)
  )

// Build the complete html list (with ul)
render() = <ul>{my_html_list()}</ul>

// Create the server
server = one_page_server("Hello", render)




相关问题
Can different OPA apps share Databases?

I m just investigating OPA and trying to make the leap from a traditional LAMP background, so here s my first of many newbie questions: Can I have two OPA apps sharing the same database, say one ...

Equivalent of List.exists for Db

I ve used List.exists( ), i woudl like to know if there is an equivalent for Db. If i have a function f(e) : bool, i would like to know if there is at least one element e, with f(e) -> true. ...

Looping over db intmap(person)

example: type person = { name : string ; age : int } db /person : intmap(person) I know how to get a single person from the db, but how do I get them all? and print ...

Can opa extensions be written in Ocaml?

I notice that included in the source of the OpaWhiteBoard example, there is a .ml file here: https://github.com/hhugo/OpaWhiteBoard/blob/master/src/opacairo/cairo.ml This appears to be OCaml with ...

Access a record field

This code source don t compile, Is there a way to make that in OPA ? type User = { nom : string ; prenom : string } un_user = { nom = "My_name" ; prenom = "My_last_name" } : User champ = "nom" do ...

OPA syntax question

I have seen in the stdlib and in some github project. Code like that : MyClass = field_id(id) = "{id}_field" {{ my_func(args) = output }} What the interest to have function before the {{ }} ...

热门标签