Was there any built-in sort function to sort an array (of string or of integer) in freepascal?
In c/c++ there is std::sort(any_arr) or qsort(any_arr) that can do this..
In Javascript or ruby there is .sort method..
What's in object-pascal?
Generic functions is a topic currently in the planning stage. Currently most list types (as benibela said) have own sort and customsort options.
Some of the generic container types (like tfpsmap in unit fgl) might have generic sort routines, but usually work with a separate compare function pointer/variable to determine order.
Related
I'm currently writing a Ruby class that provides a menu of base lambdas that can be mixed-and-matched to create a new set of lambdas. (it's an evolutionary algorithm that requires a lot of customizing of the fitness function depending on the dataset)
The configuration fire where this happens it full of stuff like this
function_from_modifier.(base_function, either.(modifier_from.(case),->(x){x}) )
The identity function ->(x){x} pops up several times in the configuration file, and it looks ugly, so I was wondering if there is a more elegant way of doing this. Is something like Elixir's &(&1) possible in Ruby?
tl;dr summary: there is no identity function in the Ruby core or standard libraries. In fact, there are no functions (in the sense of pre-defined Proc instances) at all anywhere in the core or standard libraries.
First-class functions in Ruby are kind-of second-class (if that makes sense).
When Yukihiro "matz" Matsumoto first designed Ruby, he surveyed the standard libraries of other languages for uses of first-class and higher-order functions, and he found that the vast majority of uses were:
a single function argument
that is not stored, passed, or returned
that is only immediately invoked one or more times
A significant portion of higher-order functions where this is not true are control structures (e.g. if which takes a condition and two consequences), which however he wanted to model as built-in language constructs, not library functions.
Therefore, he decided to optimize Ruby for the common case that he identified, and created blocks.
Blocks are not first-class functions:
they aren't objects
you can't send messages to them
they can't be stored in variables
they can't be returned
they can't be freely passed as arguments, you can only pass at most one and only at a special place in the argument list
As a result, real (in the sense that they are actual objects) first-class functions (Procs) are in some sense second-class language features compared to blocks:
they are more expensive in memory
calling them is slower
they are more syntactically cumbersome to create
So, in essence, it is not surprising that you are running into limitations when trying to use Ruby the way you do: that's not what Ruby was designed for.
In the past, I used to carry around a helper library with constants such like this:
class Proc
Id = -> x { x }
end
But I stopped doing that. If I want to use Ruby, I use Ruby as an OO language, if I want to do fancy FP, I use Haskell or Scala or Clojure or Racket or …
There is no anonymous functions capture in ruby, because in OOP there are objects having methods defined on them, not functions.
The most similar call would be Object#itself, and while one might do method(:itself) instead of ->(x) { x }, it would not be exactly same for many reasons.
Why would not you just assign the anonymous function to the variable and use it instead, like λ = ->(x) { x } and then use λ everywhere?
Sidenote: using a bare function instead of the block in call to either looks like a bad design to me. Blocks are faster, and everywhere in the core lib ruby uses blocks in such a case.
In my project I have many collection (slices) of different data types. Any particular collection should define fields it can sort by. I want to write one sort function and call it with user input (sort field and order) whenever collection should be sorted. I came up with the following boilerplate code (described only one type of collection, but for others it will be the same): https://gist.github.com/abonec/f1ee23a38e78ea48d470c39885de47ba
I have an interface Sortable for collections. Sortable should be passed to the sortStats with user input of sort field. If concrete implementation supports this type of sort it should return sort.Interface with corresponding interface.
Problem with many repeated implementations of sort.Interface where Len() and Swap() method is identical. Different is only Less().
Is there any approach to get a rid of Len() and Swap() methods in this case or may be other approach to write generic sort function with dynamic sort field?
You're looking for generics, which Go doesn't currently support. See this FAQ entry.
The Go team is working to add generics to the language - it's a work in progress, and everyone is free to participate in the discussion. Once generics exist, they will provide the solution you seek here.
In the meantime, you could use code generation or think of a slightly different design for your problem. Some code duplication is OK too, Go doesn't frown upon it as badly as some other languages.
I need to implement a link list data structure for my molecular dynamics code in fortran 2003/2008 I am using the newest fortran compilers (Intel).
How do I come about implement the linked list in the best way possible I would prefer a lock-free no wait implementation if possible in Fortran.
Thank you.
It is easiest if you create a user defined type with your data items and the pointer to the next item. This is assuming a singly-linked list. e.g.,
type MyList_type
integer :: FirstItem
real :: SecondItem
etc
type (MyList_type), pointer :: next_ptr => null ()
end type MyList_type
Then create the first member with "allocate". Thereafter you write code to traverse the list, using next_ptr to step through the list. Use the "associated" intrinsic function to test whether next_ptr is defined yet, or instead you have reached the end of the list.
If you are writing an ordinary sequential Fortran program then lock-free/no-wait is not an issue. If you are writing a multi-threaded / parallel program, then consistent access to the variables is an issue.
Here are some more examples: http://fortranwiki.org/fortran/show/Linked+list.
Even better, linked lists in Fortran are clearly explained in the book "Fortran 90/95 Explained" by Metcalf and Reid.
I am very new to Windows. While I was working with WMI, I saw there was no use of the term iterator rather enum or enumurator has been used for the same purpose. Do they really have iterators ? or they replace the term, iterator with enum or enum, EnumVariant etc ..... Or I am missing some thing about iterator and enumurator. As far I knew Traditionally the term enum is not same as iterator. Am I wrong ?
Enum is both a thing (a list of possible values) and an action (stepping through each item in a list). The Windows API uses both terms, relying on context to differentiate them.
As a general rule, function and interface names with "Enum" in their name mean enumerate, e.g. EnumWindows means enumerate windows and IEnumUnknown (a COM interface) means enumerate unknown [objects].
The Windows API has no single enumeration methodology. EnumWindows implements the loop internally and repeatedly calls you back via a handler function while IEnumUnknown requires the caller to write the loop using a Next() function.
So, on Windows, an enumerator is a broad class of solutions to the problem of walking through a list of elements.
Iterators are the C++ standard library concept of an enumerator. Choosing 'iterator' instead of 'enumerator' was probably done intentionally to avoid confusion with the existing enum language concept.
Unlike Windows, the C++ standard library iterator concept is very well defined: all iterators work like pointers; all iterators require the caller to write the loop, etc. There are a few classes of iterators in the C++ standard library that allow accessing elements linearly, in reverse, or randomly.
The term enumerator is often used as a synonym for iterator.
An enum, or enumeration, is something else altogether.
I have Googled ASP Classic implementations of the natural sort algorithm to no avail. Best I was able to find was from Dave Koelle, which was from a question on SO. Issue is that since I need to implement this algorithm in ASP Classic, I don't have access to certain functions such as
Collections.sort(your list, new AlphanumComparator());
Ideally, I'd like to pass an array to a function and have it return to me the ordered array.
Any ideas as to what I could do?
Thank you.
You haven't specified which language you are using in ASP. Typically this would be VBScript.
However if you were to use JScript instead then you can use JScript's array object and use its sort method. This method optionally takes as a parameter a comparator function.
var a = new Array();
// code to populate array
a.sort(function() { // Comparator code returning (-1|0|1) });
There is no need to convert everything to JScript, you can use utilities written in JScript from VBScript.