implementation

Why does my lazy filtered list in scheme consume so much memory?

雨燕双飞 提交于 2021-02-10 21:54:10
问题 I'm currently learning to use some slightly more advanced features of scheme, and I've hit a road-block with lazy lists. Basically, I'm trying to create an infinite, lazily generated list, and apply a lazy filter on it, and only take a single element. My hope was that this would consume very little memory: the filter looks at only one element at a time, and there's no need to store the previous entries. Here is my attempt at this: (define lazy-inf-seq (lambda (start next) (delay (cons start

Prevent Cast Between Two Interfaces

本秂侑毒 提交于 2021-02-10 15:40:19
问题 I have an internal class which contains implementation details, including properties, which should not be publicly accessible. I have to pass this class to public callers, and allow them to pass it around, store it, serialize it, or do basically whatever they want with it. The class can be accessed publicly in two different ways. These ways are exposed as two public interfaces. I would like to prevent the user from casting between the two public interfaces. Here is some code to demonstrate

Prevent Cast Between Two Interfaces

孤者浪人 提交于 2021-02-10 15:40:02
问题 I have an internal class which contains implementation details, including properties, which should not be publicly accessible. I have to pass this class to public callers, and allow them to pass it around, store it, serialize it, or do basically whatever they want with it. The class can be accessed publicly in two different ways. These ways are exposed as two public interfaces. I would like to prevent the user from casting between the two public interfaces. Here is some code to demonstrate

Most efficient (1-loop) way to use Priority Queue in Parallel Jobs task

萝らか妹 提交于 2021-02-08 10:32:08
问题 I am having a hard time with a data-structure related problem. I've tried quite a lot recently, but I don't know how to proceed. The problem is that I have the right output, but the timing is too slow and I don't pass the automated tests. To solve the problem, I am using a min-heap to implement the priority queue with next free times for the workers -- how could I make it more efficient? Efficiency is critical here. Task description You have a program which is parallelized and uses m

Implement ceil() in C

有些话、适合烂在心里 提交于 2021-01-27 15:11:23
问题 I want to implement my own ceil() in C . Searched through the libraries for source code & found here, but it seems pretty difficult to understand. I want clean & elegant code. I also searched on SO, found some answer here. None of the answer seems to be correct. One of the answer is: #define CEILING_POS(X) ((X-(int)(X)) > 0 ? (int)(X+1) : (int)(X)) #define CEILING_NEG(X) ((X-(int)(X)) < 0 ? (int)(X-1) : (int)(X)) #define CEILING(X) ( ((X) > 0) ? CEILING_POS(X) : CEILING_NEG(X) ) AFAIK, the

LL(1) parser implemented with stack: how to build AST?

纵饮孤独 提交于 2021-01-02 06:08:25
问题 I am currently building a parser by hand. It is a LL(1) parser. At the moment, it is a great recognizer: its function parse(List tokens) decides whether or not tokens is a member of the language or not. Now, I want to build the corresponding AST for that input. However, I know how to implement it in a recursive descent way (already did it). That is, for the challenge, I implement my stack using a stack with the classical algorithm: next <- first token of the input stack <- START_SYMBOL do {

C#: SkipLast implementation

非 Y 不嫁゛ 提交于 2020-08-24 03:44:10
问题 I needed a method to give me all but the last item in a sequence. This is my current implementation: public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source) { using (IEnumerator<T> iterator = source.GetEnumerator()) { if(iterator.MoveNext()) while(true) { var current = iterator.Current; if(!iterator.MoveNext()) yield break; yield return current; } } } What I need it for is to do something with all the items except the last one. In my case I have a sequence of objects with various

C#: SkipLast implementation

我的未来我决定 提交于 2020-08-24 03:43:27
问题 I needed a method to give me all but the last item in a sequence. This is my current implementation: public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source) { using (IEnumerator<T> iterator = source.GetEnumerator()) { if(iterator.MoveNext()) while(true) { var current = iterator.Current; if(!iterator.MoveNext()) yield break; yield return current; } } } What I need it for is to do something with all the items except the last one. In my case I have a sequence of objects with various

C#: SkipLast implementation

≡放荡痞女 提交于 2020-08-24 03:43:07
问题 I needed a method to give me all but the last item in a sequence. This is my current implementation: public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source) { using (IEnumerator<T> iterator = source.GetEnumerator()) { if(iterator.MoveNext()) while(true) { var current = iterator.Current; if(!iterator.MoveNext()) yield break; yield return current; } } } What I need it for is to do something with all the items except the last one. In my case I have a sequence of objects with various

Does a clean and extendable LSTM implementation exists in PyTorch?

生来就可爱ヽ(ⅴ<●) 提交于 2020-06-22 07:31:05
问题 I would like to create an LSTM class by myself, however, I don't want to rewrite the classic LSTM functions from scratch again. Digging in the code of PyTorch , I only find a dirty implementation involving at least 3-4 classes with inheritance: https://github.com/pytorch/pytorch/blob/98c24fae6b6400a7d1e13610b20aa05f86f77070/torch/nn/modules/rnn.py#L323 https://github.com/pytorch/pytorch/blob/98c24fae6b6400a7d1e13610b20aa05f86f77070/torch/nn/modules/rnn.py#L12 https://github.com/pytorch