投稿時間:2023-08-27 21:06:44 RSSフィード2023-08-27 21:00 分まとめ(10件)

カテゴリー等 サイト名等 記事タイトル・トレンドワード等 リンクURL 頻出ワード・要約等/検索ボリューム 登録日
TECH Techable(テッカブル) 日本酒が資産になる。熟成・冷凍した日本酒と交換できるNFTのマーケットプレイス「SAKE WORLD NFT」 https://techable.jp/archives/217903 sakeworldnft 2023-08-27 11:00:55
Ruby Rubyタグが付けられた新着投稿 - Qiita 【Rails】Simple Calendarをカラム追加無しで実装しよう。 https://qiita.com/takakou/items/3db06402454c6f092816 rails 2023-08-27 20:56:30
Ruby Railsタグが付けられた新着投稿 - Qiita 【Rails】Simple Calendarをカラム追加無しで実装しよう。 https://qiita.com/takakou/items/3db06402454c6f092816 rails 2023-08-27 20:56:30
海外TECH DEV Community Conway's game of life...in pure SQL [Postgres]! 💪🏼🤯 https://dev.to/grahamthedev/conways-game-of-lifein-pure-sql-postgres-2okc Conway x s game of life in pure SQL Postgres OK for those of you who are new here I like to do things that have never been done and should never be done again The stupid idea I had this time What if we could run Conway s game of life in SQL Now they do say there is a fine line between genius and insanity I will let you decide which side of the line this idea falls on In this first part we focus on the hard part Writing a game in pure SQL In part we will put a pretty front end on it Tell me again WHY Oh this is my way of learning stuff I think of something that seems super difficult and very silly and then try and implement it It forces me to learn things that are more complex and waaaay outside of my comfort zone I think it helps me learn faster even if it is far more difficult lol And the reason for learning Postgres I am currently getting familiar with supabase io so needed to learn some Postgres and then work out how to build an API with their product I am on that bit now So need to learn silly idea this article Anyway on with the show Writing a program in Postgres I mean what do developers need really Variables loops and if statements Well Postgres has them all So using nothing but Postgres functions and a little bit of swearing as I am not great with Postgres yet I created a fully functioning game of life kind of I am still working on the visualisation part Let s break it down a little Conway s game of life If you know the game skip this section but if not here is a quick explainer The game consists of generations of squares cells on a grid At each update a new generation of squares cells are born continue to live or die Based solely on the following rules and the positions of your starting cells there are some amazing simulations that can take place Any live cell with fewer than two live neighbours dies as if by underpopulation Any live cell with two or three live neighbours lives on to the next generation Any live cell with more than three live neighbours dies as if by overpopulation Any dead cell with exactly three live neighbours becomes a live cell as if by reproduction Here is an amazing video of what you can achieve with these rules when taken to the extreme We aren t going to be that ambitious today though we just want something that functions lol Let s get to it Creating a grid So the first thing we need is a way to create a simple grid of cells and store the state of each cell in that grid Luckily we can store this grid information in a table using just columns the x coordinate column of a cellthe y coordinate row of a cellthe state of the cell alive or dead So first thing we need to do is create that table create table grid id integer PRIMARY KEY xpos integer ypos integer val integer We added an id column as a primary key too Now the fun part we have a way to store the grid info but how do we create and populate that grid Also how do we make it so we can make a grid of any size Random stateBefore we build a grid we need some initial states for our cells Now we could hard code some values with a load of INSERT statements but that is a lot of work and pretty boring So we need to generate a random number which will be either a for alive or for dead So let s write a random integer generator with a min and max and use that to create some booleans by passing Min and Max I say boolean as it isn t strictly a boolean as it isn t true or false and I am storing it as an integer create or replace function rand int min num integer max num integer RETURNS integer LANGUAGE plpgsql as BEGIN return SELECT FLOOR RANDOM max num min num min num END It may take a minute to understand if you aren t familiar with Postgres or an hour if you are like me but the code in the return statement should look familiar as well as the function declaration Grid creationNow that we have a working random integer generator we need to create a grid of X by Y columns and rows create or replace function create grid col num integer row num integer RETURNS void LANGUAGE plpgsql as BEGIN TRUNCATE grid RESTART IDENTITY CASCADE FOR col counter IN col num BY LOOP FOR row counter IN row num BY LOOP INSERT INTO grid xpos ypos val values col counter row counter rand int END LOOP END LOOP end There are two things to note here TRUNCATE grid RESTART IDENTITY CASCADE is used to clear all the data from the table grid we created earlier The RESTART IDENTITY CASCADE part is used to reset our auto incrementing ID column to each time INSERT INTO grid xpos ypos val values col counter row counter rand int notice we call our rand int function we created earlier here Now we have a table that goes something like IDxposyposvalFor a by grid Counting neighboursEarlier when we covered the rules of the game we found out what happens to a cell is dependant on the number of neighbouring cells that were alive or dead plus the state of the current cell So we need a way of returning that So to get the neighbours we need the cells in the row above this one one to the left directly above and one to the right on the previous row the two cells before and after this one on the same row the three cells in the row below this one Normally there would be a huge gotchya here For example if we were in the first cell of an array of values when we did this check we would have an issue with the previous row as it would not exist So we would have to write a load of checks and conditional statements to get around this Luckily we are working with SQL so we can rely on the fact that if a WHERE statement does not match something it just returns no value or an empty row This actually makes our neighbours checking function more simple create or replace function get neighbours count row num integer col num integer RETURNS integer LANGUAGE plpgsql as DECLARE neighbours count integer BEGIN SELECT sum val int INTO neighbours count FROM temp grid WHERE xpos row num AND ypos col num OR xpos row num AND ypos col num OR xpos row num AND ypos col num OR xpos row num AND ypos col num OR xpos row num AND ypos col num OR xpos row num AND ypos col num OR xpos row num AND ypos col num OR xpos row num AND ypos col num return neighbours count end This checks all the surrounding cells and returns a total count of neighbours whose value is alive Now onto the fun part creating the next generation Creating the next generation Now we have all we need to create the actual game engine Previously I stated there were rules However we can actually reduce those down to rules checks as long as we start with the assumption that a cell will die is in state and then only update the state to alive if we pass one of the following conditions Does the cell have alive neighbours At the same time is the current cell currently alive If both are true the cell is alive in the state continues to live Does the cell have exactly alive neighbours If so it is either born or continues to live so both result in a live state These checks actually give us the same result as the original rules albeit we are not tracking whether a cell is born or dies in any way but yet again even that would be straight forward if we added each generation to a history table So to complete our game engine given the above checks the steps we need to take are Grab the current state of the game board Loop through each cell set the default state to deadCheck the number of neighbours using our get neighbours count function if neighbours and the cell is currently alive set to or if there are exactly neighbours set to alsoupdate that row in the database next generation function vPutting it all together the function looks like this create or replace function next generation RETURNS voidLANGUAGE plpgsql as DECLARE grid row RECORD neighbours count integer current state integer new state integer BEGIN FOR grid row in SELECT FROM grid LOOP neighbours count get neighbours count grid row xpos grid row ypos current state grid row val assume that the cell dies new state if the cell is alive with neighbours it lives IF neighbours count THEN IF current state THEN new state END IF END IF if the cell has exactly neighbours it either lives or is born IF neighbours count THEN new state END IF UPDATE grid SET val new state WHERE id grid row id END LOOP end The logic is sound but this will not quite work A quick gotchya The code logic is sound but we are dealing with a database and live data So in the last step if we update the database directly then we change the state of that particular cell Then when we check the next cell it s neighbours count will be incorrect as it will now count a cell that was previously and is now for example as it has been updated and we will get incorrect behaviour To correct for this we just need to clone the current table into a temporary table Then we run the neighbours check on our temporary table and update the original table Once we are done with our temporary table we DROP it and we are done next generation function vI have marked the two places where we changed the function with comments START ADDITION and END ADDITION in the below snippet create or replace function next generation RETURNS voidLANGUAGE plpgsql as DECLARE grid row RECORD neighbours count integer current state integer new state integer BEGIN START ADDITION We added this to clone the grid table CREATE TEMP TABLE temp grid AS TABLE grid We also updated where we select the data from FOR grid row in SELECT FROM temp grid END ADDITION LOOP neighbours count get neighbours count grid row xpos grid row ypos current state grid row val assume that the cell dies new state if the cell is alive with neighbours it lives IF neighbours count THEN IF current state THEN new state END IF END IF if the cell has exactly neighbours it either lives or is born IF neighbours count THEN new state END IF UPDATE grid SET val new state WHERE id grid row id END LOOP START ADDITION We drop the temporary table to clean up DROP TABLE temp grid END ADDITIONend Getting the current state and running the gameSo we now have nearly everything we need The final thing we need is a quick function we can call to get the current state of the board create or replace function current generation RETURNS setof grid LANGUAGE plpgsql as BEGIN grab current board RETURN QUERY SELECT FROM grid end And now we can play the game Running the game So now we can run the game with functions really easily create the tableYou only need to do this once but you do need to create the table I didn t make that into a function as it is only needed once so here is a reminder create table grid id integer PRIMARY KEY xpos integer ypos integer val integer Create a new random gameWe can create a grid of X by Y cols and rows with the create grid function Let s do a by grid select create grid You can also call this function to reset the game with a new random grid Get the current initial generationWe can grab the initial generation and display it which we will do in part select current generation Generate the next generationWe call the next generation functionselect next generation Display the new generationOnce the new generation has been created we display that instead by just selecting the current generation again select current generation Repeat steps and forever Now we can just keep calling next generation and current generation to display each lifecycle of the game What is nextNext is to put a pretty interface on it It is very difficult to transpose X and Y coordinates in a list into a game board in your head However I wanted to share the main engine with you now as I think it is a great way to learn the basics of Postgres functions Important Note As I said I am still new to Postgres there may be things I am doing here that are unnecessary or even wrong Do you own research and understand that this tutorial is more to showcase programming concepts and for fun rather than a best practices tutorial What do you think Let me know what you think in the comments below Is it a for the silliness or a for how horribly inefficient and what a terrible idea it is 2023-08-27 11:52:02
海外TECH DEV Community Complex Data Types in Golang (go) https://dev.to/diwakarkashyap/complex-data-types-in-golang-go-328l Complex Data Types in Golang go let s delve into the world of complex data types in Go Arrays An array is a fixed size collection of elements of the same type Declaration var arr intKey Points Fixed size The size of an array is defined at declaration and cannot be changed Same Type All elements in the array are of the same type Contiguous Memory Arrays are stored as contiguous memory blocks Use cases When you need to allocate a fixed size collection of elements Performance critical situations due to its predictable memory layout Slices A slice is a flexible dynamic sized version of an array It provides more power flexibility and convenience compared to arrays Declaration numbers int Key Points Dynamic size Slices can grow or shrink Reference Type Unlike arrays slices are reference types Modifying the slice will reflect in other references to the same slice Underlying Array Slices are built on top of arrays Appending elements may cause Go to create a new underlying array for the slice Use cases When you need a list like structure without knowing its size beforehand Manipulating sequences of elements with the convenience of built in methods Maps Maps are unordered collections of key value pairs They are similar to dictionaries or hash tables in other languages Declaration m map string int apple banana Key Points Unordered Iterating over map entries doesn t guarantee any specific order Unique Keys Each key in the map is unique Reference Type Like slices maps are reference types Use cases Associating keys with values for efficient look up Implementing structures like caches sets by using a map with empty structs as values Structs A struct is a composite data type that groups together zero or more fields with heterogenous types under a single type name Declaration type Person struct Name string Age int john Person John Doe Key Points Grouping Data Structs are useful to combine different fields related to a single entity Value Type Unlike slices and maps structs are value types Use cases Modeling and representing objects or more complex data types Grouping related fields together like representing a database record or request response formats Differences Fixed vs Dynamic Arrays are fixed size slices are dynamic Value vs Reference Arrays and structs are value types modifications won t affect copies Slices and maps are reference types modifications reflect on every reference Use Cases Use arrays for fixed size collections slices for dynamic collections maps for key value storage and structs for grouping related fields Understanding when to use each of these types can significantly enhance code clarity maintainability and performance Thank you for reading I encourage you to follow me on Twitter where I regularly share content about JavaScript and React as well as contribute to open source projects and learning golang I am currently seeking a remote job or internship Twitter GitHub Portfolio 2023-08-27 11:27:06
Apple AppleInsider - Frontpage News Crime blotter: Chaos near Apple Store in London https://appleinsider.com/articles/23/08/27/crime-blotter-chaos-near-apple-store-in-london?utm_medium=rss Crime blotter Chaos near Apple Store in LondonIn the latest Apple Crime Blotter Rudy Giuliani talks iCloud after his arrest Apple products are stolen from baseball and soccer stars and another big iPhone theft ring at a music festival The Apple Store on Regent Street in London The latest in an occasional AppleInsider series looking at the world of Apple related crime Read more 2023-08-27 11:18:54
海外科学 NYT > Science Covid Closed the Nation’s Schools. Cleaner Air Can Keep Them Open. https://www.nytimes.com/2023/08/27/health/schools-indoor-air-covid.html buildings 2023-08-27 11:28:15
ニュース BBC News - Home Wagner boss Prigozhin confirmed dead in plane crash - Moscow https://www.bbc.co.uk/news/world-europe-66632924?at_medium=RSS&at_campaign=KARANGA officials 2023-08-27 11:44:53
ニュース BBC News - Home Spain FA activates sexual violence policy after Hermoso kiss https://www.bbc.co.uk/sport/football/66633123?at_medium=RSS&at_campaign=KARANGA protocol 2023-08-27 11:47:26
ニュース BBC News - Home Wilko: New bid emerges for stricken retail chain https://www.bbc.co.uk/news/business-66632468?at_medium=RSS&at_campaign=KARANGA chaina 2023-08-27 11:46:39

コメント

このブログの人気の投稿

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2020-12-01 09:41:49 RSSフィード2020-12-01 09:00 分まとめ(69件)