-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'dist/main'
- Loading branch information
Showing
20 changed files
with
437 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
Title: Filter Vector | ||
Description: Filters a vector using a predicate function. | ||
Author: majvax | ||
Tags: array,filter,c++23 | ||
--- | ||
|
||
```cpp | ||
#include <ranges> | ||
#include <vector> | ||
|
||
template <typename T, typename P> | ||
auto filter(const std::vector<T>& vec, P&& predicate) { | ||
return vec | ||
| std::views::filter(std::forward<P>(predicate)) | ||
| std::ranges::to<std::vector<T>>(); | ||
} | ||
|
||
|
||
|
||
// Usage: | ||
std::vector<int> vec = {1, 2, 3, 4, 5}; | ||
std::vector<int> filtered = filter(vec, [](int i){ return i % 2 == 0; }); | ||
// filtered contains 2 and 4 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
Title: Transform Vector | ||
Description: Transforms a vector using a function. | ||
Author: majvax | ||
Tags: array,transform,c++23 | ||
--- | ||
|
||
```cpp | ||
#include <ranges> | ||
#include <vector> | ||
|
||
template <typename T, typename F> | ||
auto transform(const std::vector<T>& vec, F&& transformer) { | ||
using U = std::invoke_result_t<F, T>; | ||
return vec | ||
| std::views::transform(std::forward<F>(transformer)) | ||
| std::ranges::to<std::vector<U>>(); | ||
} | ||
|
||
|
||
|
||
// Usage: | ||
std::vector<int> vec = {1, 2, 3, 4, 5}; | ||
std::vector<int> transformed = transform(vec, [](int i){ return i * 2; }); | ||
// transformed contains 2, 4, 6, 8, 10 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
Title: Find files recursively | ||
Description: Find all the files in a directory and subdirectories using a predicate function. | ||
Author: majvax | ||
Tags: filesystem,file_search,c++17 | ||
--- | ||
|
||
```cpp | ||
#include <filesystem> | ||
#include <vector> | ||
#include <string> | ||
|
||
template <typename P> | ||
std::vector<std::filesystem::path> find_files_recursive(const std::string& path, P&& predicate) { | ||
std::vector<std::filesystem::path> files; | ||
std::error_code ec; | ||
|
||
if (!std::filesystem::exists(path, ec) || ec) | ||
return files; | ||
if (!std::filesystem::is_directory(path, ec) || ec) | ||
return files; | ||
|
||
auto it = std::filesystem::recursive_directory_iterator(path, ec); | ||
if (ec) | ||
return files; | ||
|
||
for (const auto& entry : it) | ||
if (!std::filesystem::is_directory(entry) && predicate(entry.path())) | ||
files.push_back(entry.path()); | ||
|
||
return files; | ||
} | ||
|
||
|
||
|
||
// Usage: | ||
|
||
// Find all files with size greater than 10MB | ||
auto files = find_files_recursive("Path", [](const auto& p) { | ||
return std::filesystem::file_size(p) > 10 * 1024 * 1024; | ||
}); | ||
|
||
// Find all files with ".pdf" as extension | ||
auto files = find_files_recursive("Path", [](const auto& p) { | ||
return p.extension() == ".pdf"; | ||
}); | ||
|
||
// Find all files writed after The New Year | ||
#include <chrono> | ||
// need std=c++20 | ||
auto jan_1_2025 = std::filesystem::file_time_type::clock::from_sys( | ||
std::chrono::sys_days{std::chrono::year{2025}/std::chrono::month{1}/std::chrono::day{1}} | ||
); | ||
auto files = find_files_recursive("Path", [jan_1_2025](const auto& p) { | ||
return std::filesystem::last_write_time(p) > jan_1_2025; | ||
}), | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
Title: Find files | ||
Description: Find all the files in a directory using a predicate function. | ||
Author: majvax | ||
Tags: filesystem,file_search,c++17 | ||
--- | ||
|
||
```cpp | ||
#include <filesystem> | ||
#include <vector> | ||
#include <string> | ||
|
||
template <typename P> | ||
std::vector<std::filesystem::path> find_files(const std::string& path, P&& predicate) { | ||
std::vector<std::filesystem::path> files; | ||
std::error_code ec; | ||
|
||
if (!std::filesystem::exists(path, ec) || ec) | ||
return files; | ||
if (!std::filesystem::is_directory(path, ec) || ec) | ||
return files; | ||
|
||
auto it = std::filesystem::directory_iterator(path, ec); | ||
if (ec) | ||
return files; | ||
|
||
for (const auto& entry : it) | ||
if (!std::filesystem::is_directory(entry) && predicate(entry.path())) | ||
files.push_back(entry.path()); | ||
|
||
return files; | ||
} | ||
|
||
|
||
|
||
// Usage: | ||
|
||
// Find all files with size greater than 10MB | ||
auto files = find_files("Path", [](const auto& p) { | ||
return std::filesystem::file_size(p) > 10 * 1024 * 1024; | ||
}); | ||
|
||
// Find all files with ".pdf" as extension | ||
auto files = find_files("Path", [](const auto& p) { | ||
return p.extension() == ".pdf"; | ||
}); | ||
|
||
// Find all files writed after The New Year | ||
#include <chrono> | ||
// need std=c++20 | ||
auto jan_1_2025 = std::filesystem::file_time_type::clock::from_sys( | ||
std::chrono::sys_days{std::chrono::year{2025}/std::chrono::month{1}/std::chrono::day{1}} | ||
); | ||
auto files = find_files("Path", [jan_1_2025](const auto& p) { | ||
return std::filesystem::last_write_time(p) > jan_1_2025; | ||
}), | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
Title: List Directories | ||
Description: Lists all the directories in a path. | ||
Author: majvax | ||
Tags: filesystem,directories,c++17 | ||
--- | ||
|
||
```cpp | ||
#include <filesystem> | ||
#include <vector> | ||
#include <string> | ||
|
||
std::vector<std::filesystem::path> list_directories(const std::string& path) { | ||
std::vector<std::filesystem::path> files; | ||
std::error_code ec; | ||
|
||
if (!std::filesystem::exists(path, ec) || ec) | ||
return files; | ||
if (!std::filesystem::is_directory(path, ec) || ec) | ||
return files; | ||
|
||
auto it = std::filesystem::directory_iterator(path, ec); | ||
if (ec) | ||
return files; | ||
|
||
for (const auto& entry : it) | ||
if (std::filesystem::is_directory(entry)) | ||
files.push_back(entry.path()); | ||
|
||
return files; | ||
} | ||
|
||
|
||
|
||
// Usage: | ||
auto directories = list_directories("Path"); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
title: Filter | ||
description: Filter a string with a predicate function | ||
author: majvax | ||
tags: string,filtering,c++23 | ||
--- | ||
|
||
```cpp | ||
#include <ranges> | ||
#include <string> | ||
|
||
template <typename P> | ||
std::string filter(const std::string& str, P&& predicate) { | ||
return str | ||
| std::ranges::views::filter(std::forward<P>(predicate)) | ||
| std::ranges::to<std::string>(); | ||
} | ||
|
||
|
||
|
||
// Usage: | ||
std::string str = "Hello, World!"; | ||
std::string filtered = filter(str, [](char c){ return std::isalpha(c); }); | ||
std::cout << filtered << std::endl; // HelloWorld | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
title: Palindrome | ||
description: Check if a string is a palindrome or not. | ||
author: majvax | ||
tags: string,c++23 | ||
--- | ||
|
||
```cpp | ||
#include <string> | ||
#include <ranges> | ||
#include <algorithm> | ||
|
||
bool is_palindrome(const std::string& str) { | ||
std::string sanitized_string = str | ||
| std::ranges::views::filter([](char c){ return std::isalnum(c); }) | ||
| std::ranges::views::transform([](char c){ return std::tolower(c); }) | ||
| std::ranges::to<std::string>(); | ||
|
||
return std::ranges::equal(sanitized_string, sanitized_string | std::views::reverse); | ||
} | ||
|
||
|
||
|
||
// Usage: | ||
bool pal = is_palindrome("A man, a plan, a canal, Panama"); // true | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
title: Transform | ||
description: Transform a string with a function | ||
author: majvax | ||
tags: string,transform,c++23 | ||
--- | ||
|
||
```cpp | ||
#include <ranges> | ||
#include <string> | ||
|
||
template <typename F> | ||
std::string transform(const std::string& str, F&& transformer) { | ||
return str | ||
| std::ranges::views::transform(std::forward<F>(transformer)) | ||
| std::ranges::to<std::string>(); | ||
} | ||
|
||
|
||
|
||
// Usage: | ||
std::string str = "Hello, World!"; | ||
std::string transformed = transform(str, [](char c){ return std::toupper(c); }); | ||
std::cout << transformed << std::endl; // HELLO, WORLD! | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
title: Date time formatting american | ||
description: Formats a timestamp to a human-readable date-time string in the format "MM/dd/yyyy hh:mm:ss a" | ||
author: Mcbencrafter | ||
tags: date,time,date-time,formatting,american | ||
--- | ||
|
||
```java | ||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
// using the system default time zone | ||
public static String formatDateTimeAmerican(long time, TimeUnit timeUnit) { | ||
return formatDateTimeAmerican(time, timeUnit, ZoneId.systemDefault()); | ||
} | ||
|
||
public static String formatDateTimeAmerican(long time, TimeUnit timeUnit, ZoneId timeZone) { | ||
return DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a") | ||
.withZone( | ||
timeZone != null ? timeZone : ZoneId.systemDefault() | ||
) | ||
.format(Instant.ofEpochSecond( | ||
timeUnit.toSeconds(time) | ||
)); | ||
} | ||
|
||
// Usage: | ||
System.out.println(formatDateTimeAmerican(1735689599, TimeUnit.SECONDS)); // "12/31/2024 | 11:59:59 PM" for GMT+0000 | ||
System.out.println(formatDateTimeAmerican(1735689599, TimeUnit.SECONDS, ZoneId.of("GMT+0000"))); // "12/31/2024 | 11:59:59 PM" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
title: Date time formatting european | ||
description: Formats a timestamp to a human-readable date-time string in the format "dd.MM.yyyy HH:mm:ss" | ||
author: Mcbencrafter | ||
tags: date,time,date-time,formatting,european | ||
--- | ||
|
||
```java | ||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
// using the system default time zone | ||
public static String formatDateTimeEuropean(long time, TimeUnit timeUnit) { | ||
return formatDateTimeEuropean(time, timeUnit, ZoneId.systemDefault()); | ||
} | ||
|
||
public static String formatDateTimeEuropean(long time, TimeUnit timeUnit, ZoneId timeZone) { | ||
return DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss") | ||
.withZone( | ||
timeZone != null ? timeZone : ZoneId.systemDefault() | ||
) | ||
.format(Instant.ofEpochSecond( | ||
timeUnit.toSeconds(time) | ||
)); | ||
} | ||
|
||
// Usage: | ||
System.out.println(formatDateTimeEuropean(1735689599, TimeUnit.SECONDS)); // "31.12.2024 | 23:59:59" for GMT+0000 | ||
System.out.println(formatDateTimeEuropean(1735689599, TimeUnit.SECONDS, ZoneId.of("GMT+0000"))); // "31.12.2024 | 23:59:59" | ||
``` |
Oops, something went wrong.