Elixir - ETS with custom equals/hash function - set

In Java, the equals/hash functions can be customised simply by overriding/implementing methods on a class.
This is very useful when you want to customise uniqueness of your class - so that you can check for 'duplicates' in a set easily.
How would you do the same thing in Elixir, specifically with ETS?

One way to do what I need to do is by making a unique hash function (that can return any type). There should only be one unique output of this hash function per unique input.
Then you can store the {hash, val} tuples:
table = :ets.create(:table, [])
:ets.insert(table, {hash(val), val})
:ets.lookup(table, hash(val))

Related

Returning multiple values in UDF

I have written an AggregateFactory Vertica UDF which returns a single value
getReturnTypes(si,columnTypes args,columnTypes returnTypes){
returnTypes.addVarbinary(512);
//I want to add second returnType
returnTypes.addFloat("");
}
getProtoType(si,columnTypes args,columnTypes returnTypes){
returnTypes.addVarbinary(512);
//I want to add second returnType
returnTypes.addFloat("");
}
this is not working, how can I return two values from an AggregateFactory UDF?
You cannot. User Defined Aggregate Functions (as explained in the fine manual) return ONE value per group. You might want to write a User Defined Transform Function (maybe a multi-phase Transform Function).

What's the usage of org.springframework.data.repository.query.parser.Part?

As you can see in the title , I'd appreciate it if somebody can tell the usage of the Class .
There's a inside enum Type ,how to use it?
public static enum Type {
BETWEEN(2, "IsBetween", "Between"), IS_NOT_NULL(0, "IsNotNull", "NotNull"), IS_NULL(0, "IsNull", "Null"), LESS_THAN(
"IsLessThan", "LessThan"), LESS_THAN_EQUAL("IsLessThanEqual", "LessThanEqual"), GREATER_THAN("IsGreaterThan",
"GreaterThan"), GREATER_THAN_EQUAL("IsGreaterThanEqual", "GreaterThanEqual"), BEFORE("IsBefore", "Before"), AFTER(
"IsAfter", "After"), NOT_LIKE("IsNotLike", "NotLike"), LIKE("IsLike", "Like"), STARTING_WITH("IsStartingWith",
"StartingWith", "StartsWith"), ENDING_WITH("IsEndingWith", "EndingWith", "EndsWith"), NOT_CONTAINING(
"IsNotContaining", "NotContaining", "NotContains"), CONTAINING("IsContaining", "Containing", "Contains"), NOT_IN(
"IsNotIn", "NotIn"), IN("IsIn", "In"), NEAR("IsNear", "Near"), WITHIN("IsWithin", "Within"), REGEX(
"MatchesRegex", "Matches", "Regex"), EXISTS(0, "Exists"), TRUE(0, "IsTrue", "True"), FALSE(0, "IsFalse",
"False"), NEGATING_SIMPLE_PROPERTY("IsNot", "Not"), SIMPLE_PROPERTY("Is", "Equals");
// Need to list them again explicitly as the order is important
// (esp. for IS_NULL, IS_NOT_NULL)
private static final List<Part.Type> ALL = Arrays.asList(IS_NOT_NULL, IS_NULL, BETWEEN, LESS_THAN, LESS_THAN_EQUAL,
GREATER_THAN, GREATER_THAN_EQUAL, BEFORE, AFTER, NOT_LIKE, LIKE, STARTING_WITH, ENDING_WITH, NOT_CONTAINING,
CONTAINING, NOT_IN, IN, NEAR, WITHIN, REGEX, EXISTS, TRUE, FALSE, NEGATING_SIMPLE_PROPERTY, SIMPLE_PROPERTY);
...}
Part is internal to Spring Data. It is not intended to be used by client code. So if you don't implement your own Spring Data Modul you shouldn't use it at all nor anything inside it.
A Part is basically an element of an AST that will probably result in an element of a where clause or equivalent depending on the store in use.
E.g. if you have a method findByNameAndDobBetween(String, Date, Date) parsing the method name will result in two parts. One for the name condition and one for the DOB between condition.
The type enum lists all the different types of conditions that are possible.
The parameters of the elements are the number of method arguments required and (possibly multiple) Strings that identify this type inside a method name.

How to average out value returned by an instance method for collection?

I have a simple method inside a model:
def term_months
((started_at - injected_at) / 1.month).to_i
end
This returns a simple integer.
In my View, I have a collection of this model type and I want to average out the results of each model's term_months value.
If this were a column, I could use something like #terms.average(:term_months), but this isn't the case.
Is there some way to average them out inline?
You 'll have to do it manually with a map:
#terms.map(&:term_months).inject(:+).to_f / #terms.length
What you can do is define that as a class method on Term
def self.average_term_months
scoped.map(&:term_months).inject(:+).to_f / scoped.length
end
and use it as #terms.average_term_months
This method is not for use as a classic class method, but more as a scope. However I do not define it as a scope because (personal taste here) I want scopes to be chainable.
#terms.sum(&:term_months).to_f / #terms.size
if started_at and injected_at are columns in your DB, then below would is possible has a better performance than using Enumerable methods (:sum) as it delegates the averaging to the DB and just returns an integer/float object then term_months would not be required:
Model.average("(started_at - injected_at)/ #{1.month}") #where Model is the name of your ActiveRecord Object
You might consider using the quickstats gem, which is designed to update basic statistics on an observation by observation basis as new observations become available. This can be very useful if the dataset is large and you just want the summary stats without having to retain all of the individual observations. Quickstats uses recurrence relationships Xbar(n+1) <- f(Xbar(1), x_n) and s^2(n+1) <- g(s^2(n), x_n), where Xbar(n) and s^2(n) the average and sample variance, respectively, based on n observations; x_n is the nth observation; and f and g represent the appropriate update functions.

Returning multiple values from a method

I have a method drive that goes like this:
public double drive(double milesTraveled, double gasUsed)
{
gasInTank -= gasUsed;
return totalMiles += milesTraveled;
}
I know I can't return multiple values from a method, but that's kind of what I need to do because I need both of these values in my main method, and as it is now it's obviously only returning the one. I can't think of anything that would work. Sorry if this is a super beginner question. What can I do to get both values to return from the method?
You can return multiple value from a function. To do this You can use structure.
In the structure you can keep required field and can return structure variable after operation.
You can also make a class for the required field if You are using OOPS supporting language but Structure is best way.
In most languages you can only return a single value from a method. That single value could be a complex type, such as a struct, array or object.
Some languages also allow you to define output parameters or pass in pointers or references to outside storage locations. These kinds of parameters also allow you to return additional values from your method.
not sure, but can you take array of your values?
array[0]=gasInTank;
array[0] -= gasUsed;
array[1]=milesTraveled;
array[1] -= milesTraveled;
return array;

Question about a terminology which implements something like this list.Add(new{a=1, b=2})

What is the terminology for the usage of "new" in:
list.Add(new{a=1, b=2})
And what type should I replace the T in List getList if I want to use the list as the returned value? I don't want to replace T with "object" because I want to parse it in Linq query.
Thanks.
Since you did not specify a type: new {1), it's called object initializers with anonymous types. (MSDN Explaining them) The Object Initializer part is where you do { a=1, b=2}.
If you want to be able to reference a type, you will have to create a type and stuff the values in.
list.Add(
new MyType() {
a=1,
b=2
});
If you are just going to be pairing two items look into using the Pair Class. There is also a Triplet Class just in case you might want to store 3 items.

Resources