extension-methods

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

How do I use promises in a Chrome extension?

陌路散爱 提交于 2020-06-22 02:59:07
问题 What I am trying to do is create a chrome extension that creates new, nested, bookmark folders, using promises. The function to do this is chrome.bookmarks.create() . However I cannot just loop this function, because chrome.bookmarks.create is asynchronous. I need to wait until the folder is created, and get its new ID, before going on to its children. Promises seem to be the way to go. Unfortunately I cannot find a minimal working example using an asynchronous call with its own callback like

How to put chrome extension under the task context?

一世执手 提交于 2020-05-17 06:23:26
问题 My extension is BBox Counter , i want to put BBox Counter under the task context like AdGuard AdBlocker. manifest.json { "name": "BBox Counter", "version": "2.0", "manifest_version": 2, "content_scripts": [ { "match_about_blank": true, "all_frames": true, "matches": ["<all_urls>"], "js": ["content.js"] } ], "browser_action": { "default_icon": "16.png", "default_popup": "popup.html", "default_title": "BBox Counter" }, "permissions": [ "activeTab", "contextMenus", "storage", "identity" ],

R: Expand an S3 method defined in another package

房东的猫 提交于 2020-05-13 13:35:46
问题 Let's say that package A defines and documents foo() and implements it for bar1 objects. In a new package B , I would like to expand this method and add its support for bar2 objects. Currently, I start by reexporting the method: #' @rdname foo #' @importFrom A foo #' @export A::foo And then went on extending its behaviour: #' @rdname foo #' @method foo bar2 #' @export foo.bar2 <- function(x, newparam = 3.14, ...){ dosomething(x, newparam) } Unfortunately, it seems like this creates a conflict

R: Expand an S3 method defined in another package

Deadly 提交于 2020-05-13 13:35:14
问题 Let's say that package A defines and documents foo() and implements it for bar1 objects. In a new package B , I would like to expand this method and add its support for bar2 objects. Currently, I start by reexporting the method: #' @rdname foo #' @importFrom A foo #' @export A::foo And then went on extending its behaviour: #' @rdname foo #' @method foo bar2 #' @export foo.bar2 <- function(x, newparam = 3.14, ...){ dosomething(x, newparam) } Unfortunately, it seems like this creates a conflict

Union multiple number of lists in C#

别来无恙 提交于 2020-02-23 08:43:48
问题 I am looking for a elegant solution for the following situation: I have a class that contains a List like class MyClass{ ... public List<SomeOtherClass> SomeOtherClassList {get; set;} ... } A third class called Model holds a List<Myclass> which is the one I am operating on from extern. Now I would like to extend the Model class with a method that returns all unique SomeOtherClass instances over all MyClass instances. I know that there is the Union() method and with a foreach loop I could

Kotlin extension method as alias for long method name?

孤者浪人 提交于 2020-01-24 04:38:07
问题 I am working in Kotlin using a Kotlin-native library object containing a method whose .nameIsMuchTooLongAndIsStillNotClear . In a manner similar to typealias , I want to create an alias to the method, so I can refer to it as something .shortAndClear . To complicate matters slightly, these functions have several parameters, many of which have defaults that I'd prefer not to pre-process in a wrapper. After further research, it still seems like an extension function is the way to go. To use an

how to write extension method for paging in mvc

拥有回忆 提交于 2020-01-24 04:14:05
问题 i've define static class to enable paging : public static class Pager { public static IEnumerable<T> PageData<T>(this IEnumerable<T> source, int currentPage, int pageSize) { var sourceCopy = source.ToList(); if (sourceCopy.Count() < pageSize) { return sourceCopy; } return sourceCopy.Skip((currentPage - 1) * pageSize).Take(pageSize); } } and i want in my controller to do like : var pagedDataCourses = products.OrderBy(p => p.productName).PageData(currentPage, pageSize); so where i can put that

Polymorphism Through Extension Methods?

狂风中的少年 提交于 2020-01-22 13:48:46
问题 I have a class library which contain some base classes and others that are derived from them. In this class library, I'm taking advantage of polymorphism to do what I want it to do. Now in a consuming application, I want to change the behavior of some code based on the runtime type of the child classes. So assume the following: public class Base { } public class Child1 : Base { } public class Child2 : Base { } Now in the consuming application I want do something as follows (note that all of