Creating dictionary of functions with symbols as keys - julia

I am trying to create a dictionary of functions with symbols as keys but I am getting an error. I have tried the following:
functions = Dict{
:gauss => (v::Float64)->gauss(v, 0.0, 1.0),
:sin => (v::Float64)-> sin(v),
:nsin => (v::Float64)->(-sin(v)),
:cos => (v::Float64)-> cos(v),
:ncos => (v::Float64)->(-cos(v)),
:tanh => (v::Float64)->tanh(v),
:sigm => (v::Float64)->sigmoid(v),
:id => (v::Float64)->id(v)
}
The error I am getting :
ERROR: LoadError: TypeError: in Type, in parameter, expected Type, got Pair{Symbol,getfield(Main, Symbol("##105#113"))}
Please let me know what I am doing wrong. Thanks for the help in advance.

I figured the{} need to replaced by ().

As you found out your yourself, the {} brackets indicate type parameters whereas the paranthesis indicate a constructor call.
Note, that the ::Float64 type annotations aren't necessary for your functions to be performant. Think of them more as a user interface restriction; that is users won't be able to call your methods with non-Float64s. However, if you want to specify types explicitly, you could also specify the type of your dictionary explicitly as such Dict{Symbol, Function}(...). However, since you don't initialize the Dict empty, Julia will figure out the best type based on your input (symbol function pairs).

Related

C# CS1503 Select cannot infer type, mixed up by System.Func and Func

I am using Select() on a collection to create many transform functions. C# is complaining the Select call cannot infer the type, apparently it is expecting a System.Func<> but is getting a Func<>...? I don't see the difference. The exact error text:
Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable<Func<Foo, Foo>>' to 'System.Collections.Generic.IEnumerable<System.Func<Foo, Foo>>' [language-core]csharp(CS1503)
The code, paraphrased a bit, looks like (ExtendBy returns a Foo):
BarCollection.Select(b => ((Foo f) => f.ExtendedBy(b)))
If I specify the types related to the Select explicitly it works, but that's a hassle:
BarCollection.Select<Bar, System.Func<Foo, Foo>>(b => ((Foo f) => f.ExtendedBy(b)))
If I specify the types but use Func instead of System.Func, I get the original error as when the type is not specified:
BarCollection.Select<Bar, Func<Foo, Foo>>(b => ((Foo f) => f.ExtendedBy(b)))
So it seems like some other Func besides System.Func is getting used and confusing things. Is there a way to fix this so Select can determine the type implicitly? Both pieces of code are in the same library project, with TargetFramework set to net5.0.
For reference, here are my using statements for the file calling Select():
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;

Error: The operator '+' isn't defined for the class 'Object'

As a complete Dart beginner coming from python and javascript, I find this behavior quite strange:
var user = {'name': 'John Doe', 'birth_y': 1980};
2021-user['birth_y'] // is 41
But if the operator is on the right.
user['birth_y'] + 41 // error
The error:
Error: The operator '+' isn't defined for the class 'Object'.
- 'Object' is from 'dart:core'.
Try correcting the operator to an existing operator, or defining a '+' operator.
user['birth_y'] + 41;
^
From the error, one guesses that for maps with mixed types, entries have type 'Object', Yet:
user['birth_y'] is int // true
user['birth_y'].runtimeType // int
This behavior is also exhibited by lists of mixed types,
What am I missing?
Dart does a statically analysis of your program before running it. In this analyze phase, it will look at the data type of your variables and see if it is statically safe.
In your case you have this Map:
var user = {'name': 'John Doe', 'birth_y': 1980};
Since your Map contains different types of values, Dart will try to see which data type can be used for all your values. In this case, the only thing String and int shares is the Object type. So user is being analyzed to be of the type Map<String, Object>.
This means that when you get a object from user, the only thing the analyzer can be sure about is it is of the type Object.
So when you do:
user['birth_y'] + 41
The analyzer will assume you get a Object which does not have the + operator.
You should in general not use Map as some kind of object which contains different data types. Instead create a class which makes it possible to write type safe code.

Is there some way to specify a "not" type in flow?

Is there some way to specify a "not" type in flow? For example,
compact = (input: Array<any>): Array<notFalsey> => input.filter(i => !!i);?
Currently, there is no syntax to specify a "not" type, like $Not<string>, which would be anything but a string.
For your specific compact example, Flow's library definition for Array.prototype.filter does include a special case for using the Boolean function as the filter function. So you can write
const compactedArray = myArray.filter(Boolean);
v0.31.0 will ship with a magic type $NonMaybeType, so you will be able to write a compact function with return type Array<$NonMaybeType<T>>.

Ada Maps: no visible subprogram matches the specification for "="

I've been using Ada.Containers.Indefinite_Hased_Maps to create my own custom hashed maps, and it worked quite well until I tried to use a vector as the element type. Here is an example of the problematic code:
package String_Vectors is new Ada.Containers.Vectors(Element_Type => Unbounded_String, Index_Type => Natural);
subtype String_Vector is String_Vectors.Vector;
package Positive2StringVector_HashMaps is new Ada.Containers.Indefinite_Hashed_Maps --Compiler fails here
(Element_Type => String_Vector,
Key_Type => Positive,
Hash => Positive_Hash,
Equivalent_Keys => Positive_Equal);
Basically, I cannot Positive2StringVector_HashMaps package, because the compiler comes up with:
no visible subprogram matches the specification for "="
From what I understand, it isn't finding the equality operator for the String_Vector , am I correct? If I am, what is the proper way of implementing it? And if I'm not, what am I doing wrong??
You don’t need to implement
function “=“ (L, R : String_Vectors.Vector) return Boolean
yourself, because there already is one in String_Vectors; see ALRM A.18.2(12). So you write
package Positive2StringVector_HashMaps is new Ada.Containers.Indefinite_Hashed_Maps
(Element_Type => String_Vector,
Key_Type => Positive,
Hash => Positive_Hash,
Equivalent_Keys => Positive_Equal,
“=“ => String_Vectors.”=");
By the way, is there some reason you used Ada.Containers.Vectors on Unbounded_String rather than Indefinite_Vectors on String? (wanting to change the length of a contained string would count as a Good Reason!)

How do you emit to class that has a 'params' constructor?

Here is the definition of my Package class:
type Package ([<ParamArray>] info : Object[]) =
do
info |> Array.iter (Console.WriteLine)
member this.Count = info.Length
and here is the IL, I'm trying:
let ilGen = methodbuild.GetILGenerator()
ilGen.Emit(OpCodes.Ldstr, "This is 1")
ilGen.Emit(OpCodes.Ldstr, "Two")
ilGen.Emit(OpCodes.Ldstr, "Three")
ilGen.Emit(OpCodes.Newobj, typeof<Package>.GetConstructor([|typeof<Object[]>|]))
ilGen.Emit(OpCodes.Ret)
but this doesn't seem to work. I tried:
ilGen.Emit(OpCodes.Newobj, typeof<Package>.GetConstructor([|typeof<String>; typeof<String>; typeof<String>|]))
a well as:
ilGen.Emit(OpCodes.Newobj, typeof<Package>.GetConstructor([|typeof<Object>; typeof<Object>; typeof<Object>|]))
but it just laughs at me. What am I doing wrong?
The [<ParamArray>] attribute indicates to a compiler that a method accepts a variable number of arguments. However, the CLR doesn't really support varargs methods -- it's just syntactic sugar provided by the C#/VB.NET/F# compilers.
Now, if you take away the [<ParamArray>], what are you left with?
(info : Object[])
That is the signature of the constructor you're trying to call.
So, you'll need to use the newarr and stelem opcodes to create an array, store the values into it, then call the constructor using the array as the argument. This should do what you want (though I haven't tested it):
let ilGen = methodbuild.GetILGenerator()
// Create the array
ilGen.Emit(OpCodes.Ldc_I4_3)
ilGen.Emit(OpCodes.Newarr, typeof<obj>)
// Store the first array element
ilGen.Emit(OpCodes.Dup)
ilGen.Emit(OpCodes.Ldc_I4_0)
ilGen.Emit(OpCodes.Ldstr, "This is 1")
ilGen.Emit(OpCodes.Stelem_Ref)
// Store the second array element
ilGen.Emit(OpCodes.Dup)
ilGen.Emit(OpCodes.Ldc_I4_1)
ilGen.Emit(OpCodes.Ldstr, "Two")
ilGen.Emit(OpCodes.Stelem_Ref)
// Store the third array element
ilGen.Emit(OpCodes.Dup)
ilGen.Emit(OpCodes.Ldc_I4_2)
ilGen.Emit(OpCodes.Ldstr, "Three")
ilGen.Emit(OpCodes.Stelem_Ref)
// Call the constructor
ilGen.Emit(OpCodes.Newobj, typeof<Package>.GetConstructor([|typeof<Object[]>|]))
ilGen.Emit(OpCodes.Ret)
NOTE: In this code, I used the dup OpCode to avoid creating a local variable to hold the array reference while storing the element values. This is only feasible because this code is fairly straightforward -- I strongly suggest you create a local variable to hold the array reference if you want to build something more complicated.

Resources