duck-typing

Generics and Duck-Typing XML in .NET?

徘徊边缘 提交于 2019-12-09 17:29:59
问题 I'm working with some XML representations of data instances. I'm deserializing the objects using .NET serialization but something in my soul is disturbed by having to write classes to represent the XML... Below is what I'd LOVE to do but I don't know if the syntax or if it is even possible: Consider the following: dim xmlObject = SomeXMLFunction() 'where some function returns an object/string representation of xml... xmlObject.SomePropertyDefinedInTheXML = SomeFunction() Any suggestions on

Duck typing in Go

北城余情 提交于 2019-12-08 19:00:29
问题 I want to write a Join function that takes arbitrary objects with a String() method: package main import ( "fmt" "strings" ) type myint int func (i myint) String() string { return fmt.Sprintf("%d", i) } func main() { parts := []myint{myint(1), myint(5), myint(6)} fmt.Println(Join(parts, ", ")) } func Join(parts []fmt.Stringer, sep string) string { stringParts := make([]string, len(parts)) for i, part := range(parts) { stringParts [i] = part.String() } return strings.Join(stringParts , sep) }

Python type annotation for custom duck type

送分小仙女□ 提交于 2019-12-07 18:03:29
问题 Python's typing module defines a number of duck types, e.g., typing.SupportsAbs to represent any type that implements the __abs__ special method. Is it possible to define custom duck types in a way such that I can use them as valid type annotations? For example, I would like to be able to annotate that an argument should be a duck-type equivalent of a threading.Lock , i.e., any object that implements acquire and release methods. Ideally, I could annotate such an argument as

scipy function always returns a numpy array

梦想的初衷 提交于 2019-12-06 10:36:27
问题 I'm encountering a scipy function that seems to return a numpy array no matter what's passed to it. In my application I need to be able to pass scalars and lists only, so the only "problem" is that when I pass a scalar to the function an array with one element is returned (when I would expect a scalar). Should I ignore this behaviour, or hack up the function to ensure that when a scalar is passed a scalar is returned? Example code: #! /usr/bin/env python import scipy import scipy.optimize

When you say Ruby is reflective, does this mainly refer to “duck typing”?

南笙酒味 提交于 2019-12-06 05:02:50
问题 I was reading a text describing Ruby and it said the following: Ruby is considered a “reflective” language because it’s possible for a Ruby program to analyze itself (in terms of its make-up), make adjustments to the way it works, and even overwrite its own code with other code. I'm confused by this term 'reflective' - is this mainly talking about the way Ruby can look at a variable and figure out whether it's an Integer or a String (duck typing), e.g.: x = 3 x = "three" # Ruby reassigns x to

Python type annotation for custom duck type

℡╲_俬逩灬. 提交于 2019-12-05 22:34:23
Python's typing module defines a number of duck types, e.g., typing.SupportsAbs to represent any type that implements the __abs__ special method. Is it possible to define custom duck types in a way such that I can use them as valid type annotations? For example, I would like to be able to annotate that an argument should be a duck-type equivalent of a threading.Lock , i.e., any object that implements acquire and release methods. Ideally, I could annotate such an argument as SupportsAcquireAndRequire or DuckLock , rather than object . You can define an abstract base class (ABC) to specify the

How to Work with Ruby Duck Typing

萝らか妹 提交于 2019-12-05 02:59:10
I am learning Ruby and I'm having a major conceptual problem concerning typing. Allow me to detail why I don't understand with paradigm. Say I am method chaining for concise code as you do in Ruby. I have to precisely know what the return type of each method call in the chain, otherwise I can't know what methods are available on the next link. Do I have to check the method documentation every time?? I'm running into this constantly running tutorial exercises. It seems I'm stuck with a process of reference, infer, run, fail, fix, repeat to get code running rather then knowing precisely what I'm

What does duckmap really do?

点点圈 提交于 2019-12-05 00:55:43
From the documentation duckmap will apply &block on each element and return a new list with defined return values of the block. For undefined return values, duckmap will try to descend into the element if that element implements Iterable . But then: my $list = [[1,2,3],[[4,5],6,7]]; say $list.deepmap( *² ); # [[1 4 9] [[16 25] 36 49]] say $list.duckmap( *² ); # [9 9] deepmap behaves pretty much as expected, but I can't really make any sense of what duckmap is doing. This question is related to this issue in perl6/doc . It can be solved with "They couldn't be more different", but I would like

Using LINQ, is it possible to output a dynamic object from a Select statement? If so, how?

巧了我就是萌 提交于 2019-12-04 22:30:46
Presently in LINQ, the following compiles and works just fine: var listOfFoo = myData.Select(x => new FooModel{ someProperty = x.prop1, someOtherProperty = x.prop2 }); public class FooModel{ public string someProperty { get; set; }; public string someOtherProperty { get; set; }; } However, the past few versions of .NET/C# have expanded the role of dynamic objects such as the ExpandoObject and I am wondering if there is a way to basically do this: var listOfFoo = myData.Select(x => new ExpandoObject{ someProperty = x.prop1, someOtherProperty = x.prop2 }); Obviously, I have already tried the

scipy function always returns a numpy array

本秂侑毒 提交于 2019-12-04 17:12:57
I'm encountering a scipy function that seems to return a numpy array no matter what's passed to it. In my application I need to be able to pass scalars and lists only, so the only "problem" is that when I pass a scalar to the function an array with one element is returned (when I would expect a scalar). Should I ignore this behaviour, or hack up the function to ensure that when a scalar is passed a scalar is returned? Example code: #! /usr/bin/env python import scipy import scipy.optimize from numpy import cos # This a some function we want to compute the inverse of def f(x): y = x + 2*cos(x)