I know how to check if an item is in a list: (when (member item list :test #'equalp)). If I have a list of structs book :
(defstruct book
name
author
)
how do I check if a book from a certain author is a member of the list (independently of the name)? I know this is a very beginner question and I did try to find the answer by myself but did not succeed.
What you are looking for is :key argument:
(find author library :test #'string= :key #'book-author)
Same with member:
(member (book-name my-book) library :test #'string= :key #'book-name)
Note that :key is not called on the item.
Related
My xtext grammar constrains a hierarchical structure. When the scope of an element in the hierarchical needs to be computed the following must be done:
get a list of element with the type A
get a list of element with the type B
add the two list to the scope. Repeat the for the parent element and add the parents scope to the current scope.
I use
scope_a = Scopes.scopeFor(list_a)
scope_b = Scopes.scopeFor(list_b, scope_a)
but get stuck when adding the parents scope to scope_b to create the final scope.
the scopeFor methods are just helpers. have a look at their code. they call
org.eclipse.xtext.scoping.Scopes.scopeFor(Iterable<? extends T>, Function<T, QualifiedName>, IScope)
which allows to pass a parent scope
On table-panel I stumbled upon the call to instantiate. Before, when reading the documentation for GUI in Racket I only saw new being used to create objects of GUI classes.
Usage of instantiate from that page:
(instantiate button%
((format "~a" j) child)
(stretchable-width #t)
(stretchable-height #t)
(callback
(lambda (button event)
(printf "~a~n" (send button get-label)))))
Usage of new in the rest of the documentation:
; Make a frame by instantiating the frame% class
(define frame (new frame% [label "Example"]))
What is the difference between the two?
Edit
I found a documentation page telling me about the difference, but I don't really understand what "by-name initialization arguments" are. Is this the same as keyword arguments?
In (define frame (new frame% [label "Example"])), [label "Example"] is a by-name initialization argument, with the argument named being label being given the value "Example". They're conceptually similar to keyword arguments but the mechanism is different and, unlike keyword arguments, they can be provided by position if you really want to. Here's the relevant docs, from https://docs.racket-lang.org/reference/createclass.html:
Initialization arguments can be provided by name or by position. The
external name of an initialization variable can be used with
instantiate or with the superclass initialization form. Those forms
also accept by-position arguments. The make-object procedure and the
superclass initialization procedure accept only by-position arguments.
Arguments provided by position are converted into by-name arguments
using the order of init and init-field clauses and the order of
variables within each clause. When an instantiate form provides both
by-position and by-name arguments, the converted arguments are placed
before by-name arguments. (The order can be significant; see also
Creating Objects.)
I need to implement a domain specific language. I have a panel and some shapes on it.
'panel' name = ID '(' title = STRING',' bgcolor = Color',' width = INT',' height = INT ')''{'((rects += Rect)| (ellipse += Ellipse)|(arcs += Arc)|)*'}'
and each shape has a unique rule with some other features. for example:
RoundRect:
'roundrectangle' name = ID '{'
(fill ?= 'filled' (fillpattern?='fillpattern' fillpaint=Paint)?)?
(stroke?='stroke' str=Stroke)?
'paint' paint=Paint
'coordination' x=INT ',' y=INT
'dimention' height=INT ',' width=INT
'arc' archeight=INT ',' arcwidth=INT
'}'
as it obvious in this DSL, I used some references. But I don't know this rules is correct or I should use cross-reference in those?
This rule works fine and I receive the correct output that I expected. But I know when a feature is not of the basic type (string, integer, and so on), it is
actually a reference (an instance of EReference),this is a containment reference, although for non-containment references, the referenced object is stored somewhere else,
for example, in another object of the same resource or even in a different resource.
And point is that a cross-reference is implemented as a non-containment reference.
I need to know when I should use cross-reference and when use containment reference?
As far as I know the difference is as following:
A containment-reference is if you want to reference to the content of a rule so it's just lazy for redefining the rule's content everytime you use the containment-reference.
A cross-reference behaves a bit different: If you use a cross-reference the parser need the user having typed in content of the rule the cross-reference refers to beforeallowing him to refer to that already typed in content.
An example would be a real programming language: A Method call would be a cross-reference as the method of this name should already be declared somewhere in the code because otherwise it doesn't exist. In contrary the normal code would be implemented as a containment-reference as it can be used (for example) within a class, a field or a method and the code you are typing in just needs to fullfill the existance of a few keywords and structures but these are only defined in the parser itself and needn't be defined by the user himself before beeing able to use them.
I hope I have illustrated it well enough so you now know about the difference and the meaning of these reference types.
Greeting Krzmbrzl
Your grammar describes the AST of your language. Therefore, a meta model is derived from your grammar. To describe references between your AST elements you can use containmend references and cross references. A containment reference is used if you want to describe a parent-child relation where the child object is "created" / declared during the parent object is created. A cross reference is used if the parent object points to a child object which is created / declared in an other parent object. To "draw a picture": A containment reference is a top -> bottom reference and a cross reference is a left -> right reference.
For example assume you have a field (private int field = 42;) or method (public void foo() {...}) declaration of a Java class. This declaration is modeled with a containment reference, because a Java class contains field and method declarations. On the other you have a statement field++; within the method body of foo(). There you use the former declared field foo and it is modeled as a cross reference.
In general I would say: Any declaration is modeled as a containment reference and any usage of an already declared whatever is modeled as a cross reference.
I am learning Racket at the moment and would like to know if the following is possible out of the box in Racket. When I create an instance of a class I use the following syntax:
(new Client% [name "John"]
[age 30])
I like the fact that I need to name the field ids when creating an instance of a class. Is there an equivalent when I am creating a struct. I scanned the Racket documentation but could not find anything about naming field ids when creating a struct.
TIA.
If you look at the documentation, you will find that struct creates a constructor for you that takes the initial values of a structure instance as positional arguments. AFAIK there is no built-in support for a "keyword"-like constructor.
Having said that, you can always do something like
(struct client (name age))
(struct client (name age) #:transparent)
(define (new-client #:name n #:age a)
(client n a))
(new-client #:age 20 #:name "me")
to get the usual function with keyword feeling.
In groovy, there are two methods namely any and find method that can be used in Maps.
Both these methods will "search" for the content that we are interested in (that is, both any and find method return whether the element is in Map or not, that is they need to search).
But within this search how do they differ?
They actually do different things. find returns the actual element that was found whereas any produces a bool value. What makes this confusing for you is the groovy truth.
Any unset (null?) value will resolve to false
def x
assert !x
So if you are just checking for false, then the returned values from both methods will serve the same purpose, since essentially all objects have an implicit existential boolean value.
(!list.find{predicate}) <> (!list.any{predicate})
However :
( list.find{predicate}) >< (list.any{predicate})
If any does not exist in Groovy API and you want to add this feature to List metClass, any implementation will be :
java.util.List.metaClass.any={Closure c->
return delegate.find(c) != null
}
Find is more general than any